Created
January 1, 2017 23:27
-
-
Save 421p/0b3ea022b0dca832b81c60593fe894c5 to your computer and use it in GitHub Desktop.
boost::asio http request
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
// url should always match template: host/path | |
std::string url_get_contents(std::string url) | |
{ | |
using namespace boost::asio; | |
ip::tcp::iostream stream; | |
auto tokens = explode("/", url); | |
auto host = tokens[0]; | |
auto request = tokens[1]; | |
stream.connect(host, "http"); | |
stream << "GET /" << request << " HTTP/1.1\r\n" | |
<< "Host: " << host << "\r\n" | |
<< "Accept: */*\r\n" | |
<< "Connection: close\r\n\r\n"; | |
stream.flush(); | |
std::string response(std::istreambuf_iterator<char>(stream.rdbuf()), std::istreambuf_iterator<char>()); | |
// skipping headers | |
auto it = boost::algorithm::find_first(response, "\r\n\r\n"); | |
return std::string(it.end(), response.end()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment