是否可以从QNetworkReply读取未解码的数据?
响应使用 gzip 编码(Content-Encoding: gzip HTTP 标头),但是当我调用readAll()方法时,它返回解码数据。我需要原始压缩数据,因为它是发送给我的。有任何想法吗?
是否可以从QNetworkReply读取未解码的数据?
响应使用 gzip 编码(Content-Encoding: gzip HTTP 标头),但是当我调用readAll()方法时,它返回解码数据。我需要原始压缩数据,因为它是发送给我的。有任何想法吗?
您必须为自己设置标题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
}