gpt4 book ai didi

c++ - 使用 gzip 压缩 POCO HttpResponse 会削减内容

转载 作者:行者123 更新时间:2023-11-30 05:05:53 27 4
gpt4 key购买 nike

我正在使用 POCO 1.7.8 编写 HTTP 服务器。问题是使用 gzip 压缩响应数据时:

std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "gzip");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_GZIP);
deflater << content;
deflater.close();

客户端的响应是:

HELLO WORLD, THIS IS LONGISH STRING

响应头:

Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: gzip
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 07:52:17 GMT

如果我将其更改为使用 ZLIB 并将 Content-Encoding 设置为 deflate,则整个响应将从服务器正确返回:

std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "deflate");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_ZLIB);
deflater << content;
deflater.close();

客户端的响应是:

HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT

响应头:

Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: deflate
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 08:07:36 GMT

我试图找到如何在 POCO 服务器中完成此操作的示例,但找不到任何示例,我现在有点受困于此。感谢您的帮助!

最佳答案

您确定为压缩响应正确设置了 Content-Length header 吗?或者尝试在调用 send() 之前启用分块传输编码。

response->setChunkedTransferEncoding(true);

关于c++ - 使用 gzip 压缩 POCO HttpResponse 会削减内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48163252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com