QNetworkAccessManager send data incomplete on windows
Solved
General and Desktop
-
I have a trouble with QNetworkAccessManager in windows. I wrote the following code to submit request ,it works on ubuntu perfectly but on windows send just 16384 bytes!! It seems request execute just once and freeze.
QString concatenated = username + ":" + pass; QByteArray hash = concatenated.toLocal8Bit().toBase64(); QString headerData = "Basic " + hash; QNetworkRequest request = QNetworkRequest(QUrl(baseURL)); request.setRawHeader("Authorization", headerData.toLocal8Bit()); request.setRawHeader("Content-Type", "application/json"); QNetworkReply * reply = nam->post(request,data); connect(reply,&QNetworkReply::uploadProgress,this,&myClass::uploadProgress);
in uploadProgress method:
qDebug() << sent << " " << total; if(total && sent){ int result = (sent*100)/total; emit uploaded(result); }
output:
16384 632054 // AND EVERY THINGS STOP UNTIL I GET QNetworkReply::RemoteHostClosedError ERROR CODE
-
I tested this code too, The result no change:
QString concatenated = username + ":" + pass;//username:password QByteArray hash = concatenated.toLocal8Bit().toBase64(); QString headerData = "Basic " + hash; QHttpMultiPart *httpMultiPart = new QHttpMultiPart(nam); QHttpPart *httpPart = new QHttpPart(); httpPart->setRawHeader("Authorization", headerData.toLocal8Bit()); httpPart->setRawHeader("Content-Type", "application/json"); httpPart->setBody(data); httpMultiPart->append(*httpPart); QNetworkReply* reply = nam->post(QNetworkRequest(QUrl(baseurl)),httpMultiPart);
-
After two days finally, I found why it happened! It because I emit the signal in uploadProgress directly! I changed the uploadProgress code like below and it works perfectly now!
qDebug() << sent << " " << total; if(total && sent){ int result = (sent*100)/total; QTimer::singleShot(5,[this,result](){ emit uploaded(result); } }