Files
boost_beast/examples/http_crawl.cpp

60 lines
1.6 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
2017-07-20 08:01:46 -07:00
#include "http_stream.hpp"
#include "urls_large_data.hpp"
2017-07-20 08:01:46 -07:00
#include <boost/asio.hpp>
#include <iostream>
using namespace beast::http;
using namespace boost::asio;
template<class String>
void
err(beast::error_code const& ec, String const& what)
2017-07-20 08:01:46 -07: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"});
stream<ip::tcp::socket> hs(ios);
connect(hs.lowest_layer(), it);
auto ep = hs.lowest_layer().remote_endpoint();
request_v1<empty_body> req;
req.method = "GET";
req.url = "/";
req.version = 11;
2017-07-20 08:01:46 -07:00
req.headers.insert("Host", host +
std::string(":") + std::to_string(ep.port()));
req.headers.insert("User-Agent", "beast/http");
prepare(req);
2017-07-20 08:01:46 -07:00
hs.write(req);
response_v1<string_body> resp;
2017-07-20 08:01:46 -07:00
hs.read(resp);
std::cout << resp;
}
catch(boost::system::system_error const& ec)
{
std::cerr << host << ": " << ec.what();
}
catch(...)
{
std::cerr << host << ": unknown exception" << std::endl;
}
}
}