Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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
so 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()));@
QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);//there is a invalid value int ContentLength = Variant2.toInt(); qDebug()<< "ContentLengthHeader:" + Variant2.toString();
so please help me ,thanks
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
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 .
did you connect to the QNetworkReply::finished() signal and checked in the connected slot?!
yes ,i have connect to the QNetworkReply::finished() signal .-