问题
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 about checking?
回答1:
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.
回答2:
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
回答3:
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.
回答4:
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 : http://doc.trolltech.com/4.6/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 :
http://doc.trolltech.com/4.6/network-broadcastsender.html
I didn't look at all of them but maybe you'll find some interesting things !
Hope it helps a bit !
回答5:
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).
来源:https://stackoverflow.com/questions/2340548/does-qnetworkmanager-get-accept-compressed-replies-by-default