18 #include <boost/beast/core.hpp>
19 #include <boost/beast/http.hpp>
20 #include <boost/beast/version.hpp>
21 #include <boost/asio/connect.hpp>
22 #include <boost/asio/ip/tcp.hpp>
27 namespace beast = boost::beast;
28 namespace http = beast::http;
29 namespace net = boost::asio;
33 int main(
int argc,
char** argv)
38 if(argc != 4 && argc != 5)
41 "Usage: http-client-sync <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
43 " http-client-sync www.example.com 80 /\n" <<
44 " http-client-sync www.example.com 80 / 1.0\n";
47 auto const host = argv[1];
48 auto const port = argv[2];
49 auto const target = argv[3];
50 int version = argc == 5 && !std::strcmp(
"1.0", argv[4]) ? 10 : 11;
56 tcp::resolver resolver(ioc);
57 beast::tcp_stream stream(ioc);
60 auto const results = resolver.resolve(host, port);
63 stream.connect(results);
67 req.set(http::field::host, host);
68 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
71 http::write(stream, req);
74 beast::flat_buffer buffer;
77 http::response<http::dynamic_body> res;
80 http::read(stream, buffer, res);
83 std::cout << res << std::endl;
87 stream.socket().shutdown(tcp::socket::shutdown_both, ec);
92 if(ec && ec != beast::errc::not_connected)
93 throw beast::system_error{ec};
97 catch(std::exception
const& e)
99 std::cerr <<
"Error: " << e.what() << std::endl;