Files
beast/examples/http_crawl.cpp
T

63 lines
1.7 KiB
C++
Raw Normal View History

2016-05-07 17:06:46 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-05-07 17:06:46 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2016-03-11 06:40:37 -05:00
#include "urls_large_data.hpp"
2016-03-11 06:40:37 -05:00
#include <beast/core/multi_buffer.hpp>
2016-06-20 10:53:31 -04:00
#include <beast/http.hpp>
2016-03-11 06:40:37 -05:00
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
2016-03-11 06:40:37 -05:00
#include <iostream>
using namespace beast::http;
using namespace boost::asio;
template<class String>
void
2016-05-04 17:27:50 -04:00
err(beast::error_code const& ec, String const& what)
2016-03-11 06:40:37 -05:00
{
std::cerr << what << ": " << ec.message() << std::endl;
}
int main(int, char const*[])
{
io_service ios;
for(auto const& host : urls_large_data())
{
try
{
ip::tcp::resolver r(ios);
auto it = r.resolve(
ip::tcp::resolver::query{host, "http"});
2016-06-20 10:53:31 -04:00
ip::tcp::socket sock(ios);
connect(sock, it);
auto ep = sock.remote_endpoint();
2016-11-20 07:32:41 -05:00
request<string_body> req;
req.method(verb::get);
req.target("/");
2016-05-07 15:18:22 -04:00
req.version = 11;
req.fields.insert("Host", host + std::string(":") +
boost::lexical_cast<std::string>(ep.port()));
req.fields.insert("User-Agent", "beast/http");
prepare(req);
2016-06-20 10:53:31 -04:00
write(sock, req);
2016-10-09 06:34:35 -04:00
response<string_body> res;
beast::multi_buffer b;
beast::http::read(sock, b, res);
2016-06-20 10:53:31 -04:00
std::cout << res;
2016-03-11 06:40:37 -05:00
}
2016-10-04 18:00:11 -04:00
catch(beast::system_error const& ec)
2016-03-11 06:40:37 -05:00
{
std::cerr << host << ": " << ec.what();
}
catch(...)
{
std::cerr << host << ": unknown exception" << std::endl;
}
}
}