Files
boost_beast/example/http-client/http_client.cpp
T

90 lines
2.5 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// 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 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
2017-07-20 08:01:46 -07:00
2017-06-14 20:26:44 -07:00
//[example_http_client
2017-06-04 17:25:55 -07:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
2017-07-20 08:01:46 -07:00
#include <boost/asio.hpp>
2017-06-17 20:17:01 -07:00
#include <cstdlib>
2017-07-20 08:01:46 -07:00
#include <iostream>
#include <string>
2017-07-20 13:40:34 -07:00
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
2017-06-19 14:41:28 -07:00
2017-07-20 08:01:46 -07:00
int main()
{
2017-06-17 20:17:01 -07:00
// A helper for reporting errors
auto const fail =
2017-07-20 13:40:34 -07:00
[](std::string what, boost::beast::error_code ec)
2017-06-17 20:17:01 -07:00
{
std::cerr << what << ": " << ec.message() << std::endl;
return EXIT_FAILURE;
};
2017-07-20 13:40:34 -07:00
boost::beast::error_code ec;
2017-06-17 20:17:01 -07:00
// Set up an asio socket
2017-07-20 08:01:46 -07:00
boost::asio::io_service ios;
2017-06-19 14:41:28 -07:00
tcp::resolver r{ios};
tcp::socket sock{ios};
2017-07-20 08:01:46 -07:00
2017-06-17 20:17:01 -07:00
// Look up the domain name
std::string const host = "www.example.com";
2017-06-19 07:28:20 -07:00
auto const lookup = r.resolve({host, "http"}, ec);
2017-06-17 20:17:01 -07:00
if(ec)
return fail("resolve", ec);
// Make the connection on the IP address we get from a lookup
boost::asio::connect(sock, lookup, ec);
if(ec)
return fail("connect", ec);
// Set up an HTTP GET request message
2017-07-03 20:33:54 -07:00
http::request<http::string_body> req{http::verb::get, "/", 11};
2017-06-19 14:41:28 -07:00
req.set(http::field::host, host + ":" +
2017-06-20 15:09:53 -07:00
std::to_string(sock.remote_endpoint().port()));
2017-07-20 13:40:34 -07:00
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.prepare_payload();
2017-07-20 08:01:46 -07:00
2017-06-17 20:17:01 -07:00
// Write the HTTP request to the remote host
2017-06-19 14:41:28 -07:00
http::write(sock, req, ec);
2017-06-17 20:17:01 -07:00
if(ec)
return fail("write", ec);
// This buffer is used for reading and must be persisted
2017-07-20 13:40:34 -07:00
boost::beast::flat_buffer b;
2017-06-17 20:17:01 -07:00
// Declare a container to hold the response
2017-06-19 14:41:28 -07:00
http::response<http::dynamic_body> res;
2017-06-17 20:17:01 -07:00
// Read the response
2017-06-19 14:41:28 -07:00
http::read(sock, b, res, ec);
2017-06-17 20:17:01 -07:00
if(ec)
return fail("read", ec);
// Write the message to standard out
std::cout << res << std::endl;
2017-06-17 20:17:01 -07:00
// Gracefully close the socket
2017-06-19 14:41:28 -07:00
sock.shutdown(tcp::socket::shutdown_both, ec);
2017-06-19 19:42:06 -07:00
// not_connected happens sometimes
// so don't bother reporting it.
//
2017-07-20 13:40:34 -07:00
if(ec && ec != boost::beast::errc::not_connected)
2017-06-17 20:17:01 -07:00
return fail("shutdown", ec);
// If we get here then the connection is closed gracefully
return EXIT_SUCCESS;
2017-07-20 08:01:46 -07:00
}
2017-06-04 17:25:55 -07:00
//]