Complete examples in beast html documentation

This commit is contained in:
Vinnie Falco
2016-04-22 11:09:44 -04:00
parent af3d721f82
commit ba7031cb3b
5 changed files with 158 additions and 34 deletions

36
examples/http_example.cpp Normal file
View File

@@ -0,0 +1,36 @@
//
// Copyright (c) 2013-2016 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)
//
#include <beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main()
{
// Normal boost::asio setup
std::string const host = "boost.org";
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver r(ios);
boost::asio::ip::tcp::socket sock(ios);
boost::asio::connect(sock,
r.resolve(boost::asio::ip::tcp::resolver::query{host, "http"}));
using namespace beast::http;
// Send HTTP request using beast
request<empty_body> req({method_t::http_get, "/", 11});
req.headers.replace("Host", host + ":" + std::to_string(sock.remote_endpoint().port()));
req.headers.replace("User-Agent", "Beast");
write(sock, req);
// Receive and print HTTP response using beast
beast::streambuf sb;
response<streambuf_body> resp;
read(sock, sb, resp);
std::cout << resp;
}