18 #include <boost/beast/core.hpp>
19 #include <boost/beast/http.hpp>
20 #include <boost/beast/ssl.hpp>
21 #include <boost/beast/version.hpp>
22 #include <boost/asio/connect.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/asio/ssl/error.hpp>
25 #include <boost/asio/ssl/stream.hpp>
30 namespace beast = boost::beast;
31 namespace http = beast::http;
32 namespace net = boost::asio;
33 namespace ssl = net::ssl;
37 int main(
int argc,
char** argv)
42 if(argc != 4 && argc != 5)
45 "Usage: http-client-sync-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
47 " http-client-sync-ssl www.example.com 443 /\n" <<
48 " http-client-sync-ssl www.example.com 443 / 1.0\n";
51 auto const host = argv[1];
52 auto const port = argv[2];
53 auto const target = argv[3];
54 int version = argc == 5 && !std::strcmp(
"1.0", argv[4]) ? 10 : 11;
60 ssl::context ctx(ssl::context::tlsv12_client);
66 ctx.set_verify_mode(ssl::verify_peer);
69 tcp::resolver resolver(ioc);
70 beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
73 if(! SSL_set_tlsext_host_name(stream.native_handle(), host))
75 beast::error_code ec{
static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
76 throw beast::system_error{ec};
80 auto const results = resolver.resolve(host, port);
83 beast::get_lowest_layer(stream).connect(results);
86 stream.handshake(ssl::stream_base::client);
90 req.set(http::field::host, host);
91 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
94 http::write(stream, req);
97 beast::flat_buffer buffer;
100 http::response<http::dynamic_body> res;
103 http::read(stream, buffer, res);
106 std::cout << res << std::endl;
109 beast::error_code ec;
111 if(ec == net::error::eof)
118 throw beast::system_error{ec};
122 catch(std::exception
const& e)
124 std::cerr <<
"Error: " << e.what() << std::endl;