用于在Boost.Asio中获取HTML内容的简单界面

时间:2023-02-03 20:57:18

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.

有很多例子如何向服务器发出HTTP请求并通过boost.asio库获得回复。但是,如果我需要自己实现它,我找不到简单界面的好例子并且想知道。

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?

例如,如果我需要获取http://www.foo.bar/path/to/default.html的内容,有没有办法在不验证URL的情况下获取内容,发出HTTP请求并解析服务器答案?

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.

有几件事我想避免使用boost.asio。

  • Avoid parsing and validating URL.
  • 避免解析和验证URL。

  • Manually creating HTTP request.
  • 手动创建HTTP请求。

  • Cutting HTTP response from HTML page content.
  • 从HTML页面内容中删除HTTP响应。

5 个解决方案

#1


10  

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

从那以后,有一个新人; C ++网络库:cpp-netlib如这里所指出的。

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.

你想用asio。我想你觉得这个lib的可移植性和易用性,所以在这种情况下cpp-netlib将是一个很好的选择。它基于与提升相同的原则,其作者旨在将其整合到提升中。

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


3  

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.)

您需要自己实现这些功能。 Boost.Asio主要是一个套接字库,可用于实现各种协议。但是,对于某些特定协议(如HTTP或SMTP),没有内置的便利功能。 (嗯,实际上有内置的DNS解析,但就是这样。)

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

但是,Boost.Asio源代码附带了HTTP客户端/服务器的预制示例,因此您可以轻松地从此开始。

#3


3  

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

boost.asio功能强大而且复杂,但可能有点过分。

Have you looked at libcurl?

你看过libcurl了吗?

#4


3  

From the person who wrote boost.asio

来自撰写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.

boost :: urdl是一个用于轻松地将URL读入字符串的库。

#5


2  

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

boost.asio不提供此类功能。但我相信有很多图书馆可以做到。例如,请参阅POCO库。

#1


10  

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

从那以后,有一个新人; C ++网络库:cpp-netlib如这里所指出的。

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.

你想用asio。我想你觉得这个lib的可移植性和易用性,所以在这种情况下cpp-netlib将是一个很好的选择。它基于与提升相同的原则,其作者旨在将其整合到提升中。

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


3  

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.)

您需要自己实现这些功能。 Boost.Asio主要是一个套接字库,可用于实现各种协议。但是,对于某些特定协议(如HTTP或SMTP),没有内置的便利功能。 (嗯,实际上有内置的DNS解析,但就是这样。)

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

但是,Boost.Asio源代码附带了HTTP客户端/服务器的预制示例,因此您可以轻松地从此开始。

#3


3  

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

boost.asio功能强大而且复杂,但可能有点过分。

Have you looked at libcurl?

你看过libcurl了吗?

#4


3  

From the person who wrote boost.asio

来自撰写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.

boost :: urdl是一个用于轻松地将URL读入字符串的库。

#5


2  

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

boost.asio不提供此类功能。但我相信有很多图书馆可以做到。例如,请参阅POCO库。