Performs an HTTP GET and prints the response
| 38 | |
| 39 | // Performs an HTTP GET and prints the response |
| 40 | void |
| 41 | do_session( |
| 42 | std::string const& host, |
| 43 | std::string const& port, |
| 44 | std::string const& target, |
| 45 | int version, |
| 46 | net::io_context& ioc, |
| 47 | net::yield_context yield) |
| 48 | { |
| 49 | beast::error_code ec; |
| 50 | |
| 51 | // These objects perform our I/O |
| 52 | tcp::resolver resolver(ioc); |
| 53 | beast::tcp_stream stream(ioc); |
| 54 | |
| 55 | // Look up the domain name |
| 56 | auto const results = resolver.async_resolve(host, port, yield[ec]); |
| 57 | if(ec) |
| 58 | return fail(ec, "resolve"); |
| 59 | |
| 60 | // Set the timeout. |
| 61 | stream.expires_after(std::chrono::seconds(30)); |
| 62 | |
| 63 | // Make the connection on the IP address we get from a lookup |
| 64 | stream.async_connect(results, yield[ec]); |
| 65 | if(ec) |
| 66 | return fail(ec, "connect"); |
| 67 | |
| 68 | // Set up an HTTP GET request message |
| 69 | http::request<http::string_body> req{http::verb::get, target, version}; |
| 70 | req.set(http::field::host, host); |
| 71 | req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); |
| 72 | |
| 73 | // Set the timeout. |
| 74 | stream.expires_after(std::chrono::seconds(30)); |
| 75 | |
| 76 | // Send the HTTP request to the remote host |
| 77 | http::async_write(stream, req, yield[ec]); |
| 78 | if(ec) |
| 79 | return fail(ec, "write"); |
| 80 | |
| 81 | // This buffer is used for reading and must be persisted |
| 82 | beast::flat_buffer b; |
| 83 | |
| 84 | // Declare a container to hold the response |
| 85 | http::response<http::dynamic_body> res; |
| 86 | |
| 87 | // Receive the HTTP response |
| 88 | http::async_read(stream, b, res, yield[ec]); |
| 89 | if(ec) |
| 90 | return fail(ec, "read"); |
| 91 | |
| 92 | // Write the message to standard out |
| 93 | std::cout << res << std::endl; |
| 94 | |
| 95 | // Gracefully close the socket |
| 96 | stream.socket().shutdown(tcp::socket::shutdown_both, ec); |
| 97 |
nothing calls this directly
no test coverage detected