]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #include <beast/http.hpp> | |
9 | #include <boost/asio.hpp> | |
10 | #include <boost/asio/ssl.hpp> | |
11 | #include <boost/lexical_cast.hpp> | |
12 | #include <iostream> | |
13 | #include <string> | |
14 | ||
15 | int main() | |
16 | { | |
17 | using boost::asio::connect; | |
18 | using socket = boost::asio::ip::tcp::socket; | |
19 | using resolver = boost::asio::ip::tcp::resolver; | |
20 | using io_service = boost::asio::io_service; | |
21 | namespace ssl = boost::asio::ssl; | |
22 | ||
23 | // Normal boost::asio setup | |
24 | std::string const host = "github.com"; | |
25 | io_service ios; | |
26 | resolver r{ios}; | |
27 | socket sock{ios}; | |
28 | connect(sock, r.resolve(resolver::query{host, "https"})); | |
29 | ||
30 | // Perform SSL handshaking | |
31 | ssl::context ctx{ssl::context::sslv23}; | |
32 | ssl::stream<socket&> stream{sock, ctx}; | |
33 | stream.set_verify_mode(ssl::verify_none); | |
34 | stream.handshake(ssl::stream_base::client); | |
35 | ||
36 | // Send HTTP request over SSL using Beast | |
37 | beast::http::request<beast::http::string_body> req; | |
38 | req.method = "GET"; | |
39 | req.url = "/"; | |
40 | req.version = 11; | |
41 | req.fields.insert("Host", host + ":" + | |
42 | boost::lexical_cast<std::string>(sock.remote_endpoint().port())); | |
43 | req.fields.insert("User-Agent", "Beast"); | |
44 | beast::http::prepare(req); | |
45 | beast::http::write(stream, req); | |
46 | ||
47 | // Receive and print HTTP response using Beast | |
48 | beast::streambuf sb; | |
49 | beast::http::response<beast::http::streambuf_body> resp; | |
50 | beast::http::read(stream, sb, resp); | |
51 | std::cout << resp; | |
52 | ||
53 | // Shut down SSL on the stream | |
54 | boost::system::error_code ec; | |
55 | stream.shutdown(ec); | |
56 | if(ec && ec != boost::asio::error::eof) | |
57 | std::cout << "error: " << ec.message(); | |
58 | } |