Can not get the header(QNetworkRequest::ContentLengthHeader) with a post request
-
i want to a Progressbar bar, so i need to know the "Content-Length" in the header while the data is coming,but when i post my http request, i find that i can not get the QNetworkRequest::ContentLengthHeader using the funiton below:
@ QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);//there is a invalid value
int ContentLength = Variant2.toInt(); qDebug()<< "ContentLengthHeader:" + Variant2.toString();@
i make sure the the header of "Content-Length" is exsit while i using the tool named smsniff.exe
the headers in smsniff.exe below:
bq. HTTP/1.1 200 OK
Server: Yaws 1.95
Date: Fri, 12 Apr 2013 08:32:38 GMT
Content-Length: 6023
Content-Type: application/octet-stream
Content-Encoding: gzipso i think it is a bug in QT 4.8.1 and QT 5.0.1
there is my code below:
@ QNetworkRequest request;
request.setUrl("http://www.baidu.com"); request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); iReply = iNetworkAccessManager.post(request, QByteArray("1234")); connect(iReply, SIGNAL(metaDataChanged()),this, SLOT(notifymetaDataChanged()));@
.........................
@void QHttpEngine::notifymetaDataChanged()
{QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);//there is a invalid value int ContentLength = Variant2.toInt(); qDebug()<< "ContentLengthHeader:" + Variant2.toString();
}@
so please help me ,thanks
-
Hi,
if you only need to show a progress bar you can also use the signals "downloadProgress":http://qt-project.org/doc/qt-4.8/qnetworkreply.html#downloadProgress or "uploadProgress":http://qt-project.org/doc/qt-4.8/qnetworkreply.html#uploadProgress of your "QNetworkReply":qt-project.org/doc/qt-4.8/qnetworkreply.html -
he,but ,you must know the signale
@"void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);"@the bytesTotal is always -1 except at last the bytesReceived == bytesTotal.
so while you are downloading something with post request, you do not know the bytesTotal,so how can you show a progressbar,you must know the bytesTotal in the headers with ContentLengthHeader,
but i can not get it with the API
@"QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);"@so it is not solutinon.
-
i think connecting to the metaDataChanged() signal is wrong anyway.
Try to use the signals suggested by me and gather the content length from the reply object in there.
You can try
@iReply->header(QNetworkRequest::ContentLengthHeader);@
or
@iReply->rawHeader ("ContentLengthHeader");@And note that the ContentLengthHeader isn't 100% accurate for progress indication, since there may be a request overhead, or requests may be compressed, etc.
-
i have try it ,but i can not also get it .
-
[quote author="zhangjunhua" date="1366101553"]i have try it ,but i can not also get it .
[/quote]did you connect to the QNetworkReply::finished() signal and checked in the connected slot?!
-
yes ,i have connect to the QNetworkReply::finished() signal .-