简体   繁体   English

QNetworkAccessManager 发送数据不完整

[英]QNetworkAccessManager send data incomplete

I have a trouble with QNetworkAccessManager in windows.我在 windows 中遇到 QNetworkAccessManager 问题。 I wrote the following code to submit request,it works on ubuntu perfectly but on windows send just 16384 bytes.!我编写了以下代码来提交请求,它在 ubuntu 上完美运行,但在 windows 上仅发送 16384 字节。! 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:uploadProgress方法中:

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

output: output:

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

After two days finally, I found why it happened!两天后,我终于找到了为什么会这样! It because I emit the signal in uploadProgress directly!这是因为我直接在uploadProgress 中发出信号! I changed the uploadProgress code like below and it works perfectly now!我像下面这样更改了uploadProgress代码,现在它可以正常工作了!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM