Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

QNetworkAccessManager send data incomplete on windows



  • 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
    


  • @aidinMC
    Is your host really consuming the bytes sent under/from Windows? Looks like a buffer has been filled but not read out?



  • How can I check it?
    May it Qt bug on windows 10?
    Because this code works perfectly on Ubuntu and I sent over 500MB data to server!



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


  • @aidinMC
    I don't know what your issue is. But is this your only code? You don't want to put in any code for, say, errorOccurred or finished or anything else?



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

Log in to reply