获取增强/野兽中的请求协议

时间:2020-09-14 08:00:56

标签: c++ boost boost-beast

我无法在boost beast网站服务器中获取请求的协议(HTTP或HTTPS)。

我可以使用:

req[http::field::host]

但协议没有这种运气。

req[http::field::protocol]

为空。

1 个答案:

答案 0 :(得分:0)

HTTPS没有http ::字段。

基本上,要使用HTTPS,您需要执行以下操作(示例):

1。创建io_context和ssl_context

boost::asio::io_context ioc;
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client)

2。从io_context和ssl :: context创建ssl流

boost::beast::ssl_stream<boost::beast::tcp_stream> stream(ioc, ctx)

3。使用最低层创建TCP连接

beast::get_lowest_layer(stream).connect(...endpoints iterator from query resolver..)

4。进行ssl握手

stream.handshake(ssl::stream_base::client);

5。现在,您可以像往常一样将请求写入流:

 http::request<http::string_body> req{http::verb::get, target, version};
 req.set(http::field::host, host);
 req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

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

检查官方增强兽examples