Created
September 14, 2015 09:26
-
-
Save ph1ee/d27a5ae82037e80f9952 to your computer and use it in GitHub Desktop.
An HTTPS client that connects to Yahoo using Boost.Asio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // g++ % -lboost_system -lpthread $(pkg-config --libs openssl) | |
| #include "boost/asio.hpp" | |
| #include "boost/asio/ssl.hpp" | |
| #include <string> | |
| using namespace boost::asio; | |
| int main(int argc, char *argv[]) { | |
| io_service service; | |
| typedef ssl::stream<ip::tcp::socket> ssl_socket; | |
| ssl::context ctx(ssl::context::sslv23); | |
| ctx.set_default_verify_paths(); | |
| // Open an SSL socket to the given host | |
| ssl_socket sock(service, ctx); | |
| ip::tcp::resolver resolver(service); | |
| std::string host("www.yahoo.com"); | |
| ip::tcp::resolver::query query(host, "https"); | |
| connect(sock.lowest_layer(), resolver.resolve(query)); | |
| // The SSL handshake | |
| sock.set_verify_mode(ssl::verify_none); | |
| sock.set_verify_callback(ssl::rfc2818_verification(host)); | |
| sock.handshake(ssl_socket::client); | |
| std::string req("GET /index.html HTTP/1.0 \r\nHost: " + host + | |
| "\r\nAccept: */*\r\nConnection: close\r\n\r\n"); | |
| write(sock, buffer(req.c_str(), req.length())); | |
| char buff[512]; | |
| boost::system::error_code ec; | |
| while (!ec) { | |
| int bytes = read(sock, buffer(buff), ec); | |
| std::cout << std::string(buff, bytes); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment