Files
boost_beast/examples/http_crawl.cpp

63 lines
1.7 KiB
C++
Raw Normal View History

//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 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 "urls_large_data.hpp"
2017-07-20 08:01:46 -07:00
2016-06-20 10:53:31 -04:00
#include <beast/core/streambuf.hpp>
#include <beast/http.hpp>
2017-07-20 08:01:46 -07:00
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
2017-07-20 08:01:46 -07:00
#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"});
2016-06-20 10:53:31 -04:00
ip::tcp::socket sock(ios);
connect(sock, it);
auto ep = sock.remote_endpoint();
request<empty_body> req;
req.method = "GET";
req.url = "/";
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);
response<string_body> res;
2016-06-20 10:53:31 -04:00
streambuf sb;
beast::http::read(sock, sb, res);
std::cout << res;
2017-07-20 08:01:46 -07:00
}
catch(beast::system_error const& ec)
2017-07-20 08:01:46 -07:00
{
std::cerr << host << ": " << ec.what();
}
catch(...)
{
std::cerr << host << ": unknown exception" << std::endl;
}
}
}