【发布时间】:2018-11-22 19:06:49
【问题描述】:
我的出发点是从 boost http_client_async 的 boost beast http_client_async 示例创建一个简单的下载器代码。在这种情况下,我想将收到的正文写入文件。
于是我把字符串body换成了file_body,用来写入接收到的数据:
http::response_parser<http::file_body> res_;
并且简单地将 on_write 方法重写为
void on_write( boost::system::error_code ec,
std::size_t bytes_transferred )
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "write");
boost::system::error_code ec_file;
res_.body().open("myTest.txt", boost::beast::file_mode::write, ec_file);
// test for ec_file missing
// Receive the HTTP response
http::async_read(socket_, buffer_, res_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
但是现在,一些接收到的数据体太大了:
read: body limit exceeded
我尝试增加身体限制。
如果使用解析器而不是消息,可以使用body_limit() 方法更改请求正文的大小限制。
是否还有一种简单的方法可以通过消息增加正文大小限制?
【问题讨论】:
-
我认为使用
response_parser<http::file_body>代替response<http::string_body>是最好的方法。这里弄糊涂了。响应解析器不是消息。当试图让 body 调用 open 时,res_ 需要调用res_.get().body().open("myTest.txt", boost::beast::file_mode::write, ec_file);。 response_parserres_能够处理成员变量 body_limit()。
标签: c++ boost-beast