qtqnetworkaccessmanager

QNetworkAccessManager send data incomplete


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

Solution

  • 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);   
        }
    }