2014-07-08 56 views
0

我用适当的Poco :: Net :: HTTPRequest使用Poco :: Net :: HTTPClientSession获取服务器页面,该工作正常。现在我收到回复,有时返回的页面会被压缩。如何在POCO :: Net :: HTTPResponse对象中获取内容编码?

我需要弄清楚什么时候是这种情况,这样我可以在必要时放气。应该指出这一点的HTTP头是Content-Encoding:gzip;但Poco :: Net :: HTTPResponse中没有getContentEncoding()方法。

这里有一个非工作片断(因为没有resp.getContentEncoding()):

// resp is the Poco::Net::HTTPResponse object, 
// sess is the Poco::Net::HTTPClientSession 
std::istream &in = sess.receiveResponse(resp); 

// Get the server-returned body as a string (potentially deflate) 
std::ostringstream serveroutput; 
if (resp.getContentEncoding() == "gzip") { 
    Poco::InflatingInputStream 
     inflater(in, Poco::InflatingStreamBuf::STREAM_GZIP); 
    Poco::StreamCopier::copyStream(inflater, serveroutput); 
} else 
    Poco::StreamCopier::copyStream(in, serveroutput); 

// Now we can get at the body as a string 
std::string txt = serveroutput.str(); 

有谁知道如何在生头得到的,这样我可以检查标题内容编码自己,或另一种有用的方法来确定服务器响应是否被gzipped?

回答

1

如果服务器设置Content-Encoding标头 ,您可以获得如下的编码值。

resp.get( “内容编码”)

相关问题