Cross-Platform HTTP library

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?
I'm on Windows.
Last edited on
Curl works on Windows. Also, you can use Boost.Asio to do it
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
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"){}
		
        std::string tmp;
        std::getline(sock, tmp, '\r');
        return tmp;// std::string(tmp.begin(), tmp.end() - 2); //strip trailing \r\n
	}
	catch (std::exception& e)
	{
		return e.what();
	}
}

I adapted this from a boost example.
Can you please give an example use?
Last edited on
std::string res = get_http_data("www.iheartquotes.com", "/api/v1/random?max_lines=4");
Doesn't work with PDF :(. Only download the version.
Last edited on
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
Topic archived. No new replies allowed.