开发者

Does QNetworkManager get accept compressed replies by default?

开发者 https://www.devze.com 2022-12-21 13:37 出处:网络
I am using QNetworkManager to fetch files from a server, however what I cannot figure out is if the files are compressed during the transfer with the standard gzip compression and if not how to get th

I am using QNetworkManager to fetch files from a server, however what I cannot figure out is if the files are compressed during the transfer with the standard gzip compression and if not how to get them to download compressed.

How would I go abo开发者_JAVA技巧ut checking?


I just ran a quick test by adding:

request.setRawHeader("Accept-Encoding", "gzip,deflate");

to the QNetworkRequest and the data returns what look compressed (because its ~20% smaller and unusable).

It appears that the QNetworkManager and the QNetworkReply are not intelligent as far as decompression is concerned. It looks like I have to implement a gzip and/or deflate on the returned QByteArray.


When you set a custom Accept-Encoding raw header on a QNetworkRequest object (for example via an overridden QNetworkAccessManager::createRequest() ), QtWebKit will never decompress the reply anymore: source code of qhttpnetworkconnection.cpp : ====================

    // 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, deflate");
        request.d->autoDecompress = true;
#else
        // if zlib is not available set this to false always
        request.d->autoDecompress = false;
#endif


You should use a packet sniffer / network analyzer and check for yourself.

QNetworkAccessManager does support receiving compressed HTTP replies, so in theory it should work if the HTTP server is correctly set up.


Read this elsewhere so w/o having tested it: just don't set the accept-encoded header yourself, then QNam should handle it transparently (return decompressed payload).


Considering the following sentence, I would say no, but they can be :

The downloadProgress() signal is also emitted when data is received, but the number of bytes contained in it may not represent the actual bytes received, if any transformation is done to the contents (for example, decompressing and removing the protocol overhead).

You can find it here : https://doc.qt.io/qt/qnetworkreply.html

I didn't test it tho !

To compress, if I remember well, you can send QByteArray... And on this kind of objects, you can use "compress"...

You could also have a look at some Qt examples, like :

https://doc.qt.io/qt/qtnetwork-broadcastsender-example.html

I didn't look at all of them but maybe you'll find some interesting things !

Hope it helps a bit !

0

精彩评论

暂无评论...
验证码 换一张
取 消