如何在boost beast http请求中设置http头
How to set up http header in boost-beast http request?
我正在尝试使用boost http库发送带有标头的消息。我搜索了一种发送带有标题的消息的方法,但找不到。
我想做的是跟踪
auto const results = resolver.resolve(host, port);
beast::get_lowest_layer(stream).connect(results);
stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
// set http header ("key" = "I am a header")
// I want to add above code.
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
请让我知道正确的方式添加头以促进野兽http请求。谢谢
仅
req.set("key", "I am a header");
它与其他标准的HTTP标头几乎相同,只是使用了自定义名称。
查看Coliru直播
#include <boost/beast/http.hpp>
#include <iostream>
namespace http = boost::beast::http;
int main() {
auto verb = http::verb::get;
std::string query = "/path";
std::string data = "?whatever=more";
std::string host = "example.com";
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
req.set("key", "I am a header");
req.prepare_payload();
std::cout << req;
}
打印
GET /path?whatever=more HTTP/1.1
Host: example.com
key: I am a header
相关文章:
- 如何在boost beast http请求中设置http头
- 在多个核心中处理一个HTTP请求
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 使用 winsock 接收 http 请求
- 对于 http 请求,python 比 c++ 快吗?
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 带有C++的 HTTP 请求
- 如何在QT中同步发送http请求
- 我可以使用 Boost.Asio 和 Boost.Beast 库发出 HTTPS 请求或 HTTP/2 请求吗?
- HTTP 请求中的标头名称无效
- 套接字或 HTTP 请求
- Winsock2.h 无法发送 http 请求
- 用libcurl生成http请求作为字符串
- http请求之前和之后的垃圾
- 从主机向在VirtualBox linux机器上运行的服务器发送http请求
- 如何在不阻塞 UI 线程的情况下对C++发出 http 请求
- C++ Boost 1.66 使用 Beast http 请求解析器解析字符串
- 在新线程C++中发送 http 请求
- 有什么方法可以通过按下按钮将HTTP请求从JS发送到C
- 套接字"Resource temporarily unavailable"的原始 HTTP 请求