用于在Boost.Asio中获取HTML内容的简单界面 [英] Simple interface for getting HTML content in Boost.Asio

查看:141
本文介绍了用于在Boost.Asio中获取HTML内容的简单界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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://的内容, www.foo.bar/path/to/default.html ,有没有办法在不验证URL的情况下获取内容,发出HTTP请求并解析服务器的答案?

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>

有些事情我想避免使用boost.asio.

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

  • 避免解析和验证URL.
  • 手动创建HTTP请求.
  • 从HTML页面内容中剪切HTTP响应.

推荐答案

从那时起,有一个新来者. C ++网络库:cpp-netlib 指出了

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

您想使用asio.我想您很喜欢此库的可移植性和易用性,因此 cpp-netlib 将是在那种情况下是一个很好的选择.它基于与boost相同的原理,其作者旨在将其集成到boost中.

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.

使用非常简单:

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.

这篇关于用于在Boost.Asio中获取HTML内容的简单界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆