【发布时间】:2013-09-15 19:00:05
【问题描述】:
是否可以从 QNetworkReply 读取未解码的数据?
响应使用 gzip 编码(Content-Encoding: gzip HTTP 标头),但是当我调用 readAll() 方法时,它返回解码数据。我需要原始压缩数据,因为它是发送给我的。有什么想法吗?
【问题讨论】:
标签: c++ qt http gzip qnetworkaccessmanager
是否可以从 QNetworkReply 读取未解码的数据?
响应使用 gzip 编码(Content-Encoding: gzip HTTP 标头),但是当我调用 readAll() 方法时,它返回解码数据。我需要原始压缩数据,因为它是发送给我的。有什么想法吗?
【问题讨论】:
标签: c++ qt http gzip qnetworkaccessmanager
您必须为自己的QNetworkRequest 设置标题:
networkRequest.setRawHeader("Accept-Encoding", "gzip");
那么 Qt 不会在回复中为您解码。
我们可以在qhttpnetworkconnection.cpp的源码中看到QHttpNetworkConnectionPrivate::prepareRequest:
// If the request had a accept-encoding set, we better not mess
// with it. If it was not set, we announce that we understand gzip
// and remember this fact in request.d->autoDecompress so that
// we can later decompress the HTTP reply if it has such an
// encoding.
value = request.headerField("accept-encoding");
if (value.isEmpty()) {
#ifndef QT_NO_COMPRESS
request.setHeaderField("Accept-Encoding", "gzip");
request.d->autoDecompress = true;
#else
// if zlib is not available set this to false always
request.d->autoDecompress = false;
#endif
}
【讨论】: