Simple interface for getting HTML content in Boost

2019-06-28 08:07发布

问题:

There are a lot of examples how to make HTTP request to a server and get reply via boost.asio library. However, I couldn't find a good example of simple interface and wondering, if I need to implement it myself.

For instance, if I need to get content of http://www.foo.bar/path/to/default.html, is there any way to get a content without validating URL, making HTTP request and parsing server answer?

Basically, I am looking for something like this:

std::string str = boost::asio::get_content("http://www.foo.bar/path/to/default.html");
std::cout << str;

#
<HTML>
  <BODY>
    Simple HTML page!
  </BODY>
</HTML>

There are couple of things that I would like to avoid using boost.asio.

  • Avoid parsing and validating URL.
  • Manually creating HTTP request.
  • Cutting HTTP response from HTML page content.

回答1:

Since then, there is a newcomer; the C++ Network Library: cpp-netlib as pointed out here.

You wanted to use asio. I suppose you fancied the portability and the ease of use of this lib, so cpp-netlib will be a great choice in that case. It is based on same principles as boost and its authors aim at integrating it into boost.

It is pretty simple to use:

http::client client;
/*<< Creates a request using a URI supplied on the command line. >>*/
http::client::request request("http://www.foo.bar/path/to/default.html");
/*<< Gets a response from the HTTP server. >>*/
http::client::response response = client.get(request);
/*<< Prints the response body to the console. >>*/
std::cout << body(response) << std::endl;

I haven't tried this one but it seems to be possible to do exactly what you need:

cout << body(client().get(client::request("http://www.foo.bar/path/to/default.html")));

This question was asked a long time ago, sorry for digging it out of its grave.



回答2:

You'll need to implement these functions yourself. Boost.Asio is a socket library primarily, that can be used to implement various protocols. But there's no built-in convenience functions just for some specific protocol like HTTP or SMTP. (Well, actually there's built in DNS resolution, but that's about it.)

However, the Boost.Asio source code comes with pre-made examples of an HTTP client/server, so you can easily start with that.



回答3:

boost.asio is powerful and sophisticated, but probably overkill for this.

Have you looked at libcurl?



回答4:

From the person who wrote boost.asio

http://think-async.com/Urdl/doc/html/urdl/getting_started/integrating_with_boost_asio.html

boost::urdl is a library for reading urls into strings easily.



回答5:

boost.asio doesn't provide such functionality. But I believe there are a number of libraries that do. See POCO libraries for example.