2016-10-02 16:33:42 -04:00
|
|
|
//
|
|
|
|
// 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 <boost/asio/ssl.hpp>
|
2016-11-09 10:40:09 -05:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2016-10-02 16:33:42 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
using boost::asio::connect;
|
|
|
|
using socket = boost::asio::ip::tcp::socket;
|
|
|
|
using resolver = boost::asio::ip::tcp::resolver;
|
|
|
|
using io_service = boost::asio::io_service;
|
|
|
|
namespace ssl = boost::asio::ssl;
|
|
|
|
|
|
|
|
// Normal boost::asio setup
|
|
|
|
std::string const host = "github.com";
|
|
|
|
io_service ios;
|
|
|
|
resolver r{ios};
|
|
|
|
socket sock{ios};
|
|
|
|
connect(sock, r.resolve(resolver::query{host, "https"}));
|
|
|
|
|
|
|
|
// Perform SSL handshaking
|
|
|
|
ssl::context ctx{ssl::context::sslv23};
|
|
|
|
ssl::stream<socket&> stream{sock, ctx};
|
|
|
|
stream.set_verify_mode(ssl::verify_none);
|
|
|
|
stream.handshake(ssl::stream_base::client);
|
|
|
|
|
|
|
|
// Send HTTP request over SSL using Beast
|
2016-10-09 06:34:35 -04:00
|
|
|
beast::http::request<beast::http::empty_body> req;
|
2016-10-02 16:33:42 -04:00
|
|
|
req.method = "GET";
|
|
|
|
req.url = "/";
|
|
|
|
req.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
req.fields.insert("Host", host + ":" +
|
2016-11-09 10:40:09 -05:00
|
|
|
boost::lexical_cast<std::string>(sock.remote_endpoint().port()));
|
2016-11-10 05:34:49 -05:00
|
|
|
req.fields.insert("User-Agent", "Beast");
|
2016-10-02 16:33:42 -04:00
|
|
|
beast::http::prepare(req);
|
|
|
|
beast::http::write(stream, req);
|
|
|
|
|
|
|
|
// Receive and print HTTP response using Beast
|
|
|
|
beast::streambuf sb;
|
2016-10-09 06:34:35 -04:00
|
|
|
beast::http::response<beast::http::streambuf_body> resp;
|
2016-10-02 16:33:42 -04:00
|
|
|
beast::http::read(stream, sb, resp);
|
|
|
|
std::cout << resp;
|
|
|
|
|
|
|
|
// Shut down SSL on the stream
|
|
|
|
boost::system::error_code ec;
|
|
|
|
stream.shutdown(ec);
|
|
|
|
if(ec && ec != boost::asio::error::eof)
|
|
|
|
std::cout << "error: " << ec.message();
|
|
|
|
}
|