Skip to content

Instantly share code, notes, and snippets.

@421p
Created January 1, 2017 23:27
Show Gist options
  • Save 421p/0b3ea022b0dca832b81c60593fe894c5 to your computer and use it in GitHub Desktop.
Save 421p/0b3ea022b0dca832b81c60593fe894c5 to your computer and use it in GitHub Desktop.
boost::asio http request
// 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