Poco Http Response未正确解码

时间:2013-10-05 20:01:55

标签: c++ poco-libraries

不知怎的,我发送了请求,但内容仍然看起来很奇怪,似乎它没有被解码,但响应包含以下内容: 内容编码: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;
}

1 个答案:

答案 0 :(得分:7)

要解压缩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;
...