2

不知何故,我发送了请求,但内容看起来仍然很奇怪,因为它没有被解码,但响应包含以下内容: Content-Encoding: gzip

我试图手动解码响应,但没有奏效。感谢您的帮助 :)

void Client::load_login_page()
{
using namespace Poco;
using namespace Poco::Net;

URI uri(constants::url::main_url);
//HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPClientSession session("127.0.0.1", 8888);//Support Fiddler

std::string path(uri.getPathAndQuery());
if (path.empty()) 
    path = "/";

// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.set("User-Agent",constants::url::user_agent);
req.add("Accept", "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/    x-xbitmap, */*;q=0.1");
req.add("Accept-Encoding","gzip,deflate");
req.setHost(uri.getHost(),uri.getPort());

session.sendRequest(req);

// get response
HTTPResponse res;
std::cout << res.getStatus() << " " << res.getReason() << std::endl;


std::cout << res.getContentType() << std::endl;
auto iter = res.begin();

while(iter != res.end())
{
    std::cout << iter->first << " : " << iter->second << std::endl;
    iter++;
}




std::istream &is = session.receiveResponse( res );
std::stringstream ss;
StreamCopier::copyStream( is, ss );

std::cout << ss.str() << std::endl;
}
4

1 回答 1

8

要解压缩 gzip 编码的响应,您可以使用 POCO 中提供的过滤器流包装器。

在您的标题中:

#include "Poco/InflatingStream.h"

然后,使用 std::istream 和压缩类型构造 Poco::InflatingInputStream:

std::istream &is = session.receiveResponse( res );
std::stringstream ss;
Poco::InflatingInputStream inflater(is, Poco::InflatingStreamBuf::STREAM_GZIP);
StreamCopier::copyStream( inflater, ss );

std::cout << ss.str() << std::endl;
...
于 2014-04-02T17:23:56.633 回答