QNetworkAccessManager 发送数据不完整

QNetworkAccessManager send data incomplete

我在 windows 中遇到 QNetworkAccessManager 问题。我编写了以下代码来提交请求,它在 ubuntu 上完美运行,但在 windows 上仅发送 16384 字节!似乎请求只执行一次并冻结。

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

uploadProgress方法中:

qDebug() << sent << " " << total;
if(total && sent){
    int result = (sent*100)/total;
    emit uploaded(result);
}

输出:

16384 632054 // AND EVERY THINGS STOP UNTIL I GET QNetworkReply::RemoteHostClosedError ERROR CODE

两天后,我终于找到原因了!这是因为我直接在 uploadProgress 发出信号!我像下面这样更改了 uploadProgress 代码,它现在可以完美运行了!

qDebug() << sent << " " << total;
if(total && sent){
    int result = (sent*100)/total;
    QTimer::singleShot(5,[this,result](){
       emit uploaded(result);   
    }
}