从QNetworkReply读取未解码的数据

时间:2013-09-15 19:00:06

标签: c++ qt http gzip qnetworkaccessmanager

是否可以从 QNetworkReply 中读取未解码的数据?

使用gzip( Content-Encoding:gzip HTTP标头)对响应进行编码,但是当我调用 readAll()方法时,它会返回已解码的数据。我需要原始的gzip压缩数据,因为它是发送给我的。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您必须为自己QNetworkRequest设置标题:

 networkRequest.setRawHeader("Accept-Encoding", "gzip");

然后Qt在回复中没有为您解码。

我们可以在[{1}}的来源中看到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
    }