C ++库使用有效的HTTP请求填充SSL流 [英] C++ library to populate SSL stream with valid HTTP requests

查看:65
本文介绍了C ++库使用有效的HTTP请求填充SSL流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost.Asio ssl流,并获得了一个可以使用的加密套接字,我可以从该套接字发送和接收字节.

I am using Boost.Asio ssl streams, and got a working encrypted socket from which I can send and receive bytes.

我成功使用以下代码进行了GET请求:

I successfully did a GET request with the following code :

// Construct HTTP request (using vanilla std::ostream)
std::ostream request_stream(&request);
request_stream << "GET  / HTTP/1.0\r\n";
request_stream << "Host: " << argv[1] << "\r\n";
...
// Send request
ssl::stream<tcp::socket> socket
boost::asio::write(socket, request);

现在,我找到一个小型的C ++库,该库将提供简便的方法来将有效的HTTP请求加载到ostream中

And I would now love to find a small C++ library that would provide an easy way to get the ostream loaded with a valid HTTP request !

推荐答案

由于您已经在使用Boost.Asio,请考虑使用Boost.Beast,它是一个低级HTTP库.使用Boost.Beast发送GET请求的示例:

Since you're already using Boost.Asio, consider using Boost.Beast which is a low-level HTTP library. Example of sending a GET request using Boost.Beast:

using namespace boost::beast;

// Set up an HTTP GET request message
http::request<http::empty_body> req{http::verb::get, "/", 11};
req.set(http::field::host, "www.example.com");
req.set(http::field::user_agent, "Beast/1.0);

// Send the HTTP request to the remote host
http::write(socket, req);

完整的示例在这里: https://github.com/boostorg/野兽/blob/master/example/http/client/sync/http_client_sync.cpp

Beast在Boost版本1.66和更高版本中可用.这是文档页面,其中包含许多示例: http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/index.html

Beast is available in Boost versions 1.66 and later. Here is the documentation page, which includes many examples: http://www.boost.org/doc/libs/1_66_0/libs/beast/doc/html/index.html

如果您真的想将HTTP请求写入 std :: ostream ,则Beast支持 operator<< 来处理HTTP消息(它主要用于调试),但我认为您最好使用Beast将其直接写入 ip :: tcp :: socket ssl :: stream .

If you really want to write the HTTP request to a std::ostream, Beast supports operator<< for HTTP messages (it is mainly there for debugging), but I think you are better off just writing it directly to the ip::tcp::socket or ssl::stream using Beast.

这篇关于C ++库使用有效的HTTP请求填充SSL流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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