作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 boost http 库发送带有 header 的消息。我搜索了一种发送带有 header 的消息的方法,但找不到。
我想做的就是跟随
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);
请告诉我将 header 添加到 boost-beast http 请求的正确方法。谢谢!
最佳答案
只是
req.set("key", "I am a header");
它与其他 - 标准 HTTP - header 几乎相同,但使用自定义名称。
#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
关于c++ - 如何在 boost-beast http 请求中设置 http header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62845890/
我是一名优秀的程序员,十分优秀!