May 17, 2014 at 4:25pm UTC
Hello.
I'm making a program that needs to download some files. I'm using console.
As Qt is too heavy (in this case) and Boost.Asio doesn't handle it, I need some HTTP library. Which should I use?
May 17, 2014 at 6:12pm UTC
I'm on Windows.
Last edited on May 17, 2014 at 6:12pm UTC
May 18, 2014 at 1:35am UTC
Can you please give an example use?
Last edited on May 18, 2014 at 1:38am UTC
May 18, 2014 at 2:24am UTC
std::string res = get_http_data("www.iheartquotes.com" , "/api/v1/random?max_lines=4" );
May 18, 2014 at 1:58pm UTC
Doesn't work with PDF :(. Only download the version.
Last edited on May 18, 2014 at 1:58pm UTC
May 18, 2014 at 6:37pm UTC
Try this version, it worked with a couple pdfs I tried.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
std::string get_http_data(const std::string& server, const std::string& file)
{
try
{
boost::asio::ip::tcp::iostream sock(server, "http" );
sock.expires_from_now(boost::posix_time::seconds(60));
if (!sock){ throw "Unable to connect: " + sock.error().message(); }
// ask for the file
sock << "GET /" << file << " HTTP/1.0\r\n" << "Host: "
<< server << "\r\n" << "Accept: */*\r\n" << "Connection: close\r\n\r\n" ;
// Check that response is OK.
std::string http_version;
unsigned int status_code;
sock >> http_version >> status_code;
std::string status_message;
std::getline(sock, status_message);
if (!sock && http_version.substr(0, 5) != "HTTP/" ){ throw "Invalid response\n" ; }
if (status_code != 200){ throw "Response returned with status code " + status_code + status_message; }
// Process the response headers, which are terminated by a blank line.
std::string header;
while (std::getline(sock, header) && header != "\r" ){}
// Write the remaining data to output.
std::stringstream ss;
ss << sock.rdbuf();
return ss.str();
}
catch (std::exception& e)
{
return e.what();
}
}
Last edited on May 18, 2014 at 7:05pm UTC