Friday, February 13, 2015

Boost.Asio HTTPS client

The boost asio official documentation gives two examples of making HTTP requests and using SSL for a connection. However, none of these examples do a HTTPS GET request.
In other words, if you want to make a HTTP GET request over a secure connection, you need to combine the two examples.
The links to the examples are http client and ssl client.

Let's see the difference between a HTTP client and a HTTPS client.
First, for the HTTP client the asio "flow", as per the http client example in the link above, is as follows:

Resolve

Connect

Send request

Receive response (process status, headers, body, etc.)

For a HTTPS client, a few modifications are required. First, regarding the code, one needs an ssl context boost::asio::ssl::context and the socket must support ssl. I've used boost::asio::ssl::stream<boost::asio::ip::tcp::socket>.