QNetworkAccessManager:如何确保下载完成?

时间:2016-05-03 11:49:54

标签: qt

如果我这样做:

QNetworkRequest newRequest(url);
newRequest.setUrl(url);
QNetworkReply *reply = networkManager->get(newRequest);
connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));

void myClass::onRetrievedDownLoadInfo()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());

    if (!reply)
    {
        ui->upgradeLog->appendPlainText("Download failed, invalid context");
        return;
    }

    //Remeber to delete the reply
    //---------------------------
    reply->deleteLater();

    if (reply->error() != QNetworkReply::NoError)
    {
        ui->upgradeLog->appendPlainText("Download failed, invalid context");
        return;
    }

    reply->readAll();
    ...
}

当程序到达行时,我可以说

  

reply-&GT; readAll();

服务器回复的所有内容是否已成功收到?

由于网络故障,reply->readAll();是否只能返回部分答复?

1 个答案:

答案 0 :(得分:2)

错误在于:

connect(reply, SIGNAL(finished()), this, SLOT(onRetrievedDownLoadInfo()));

你没有以正确的方式做到这一点。使用QNetworkAccessManager检查网络是否正常运行:

QNetworkConfigurationManager manager;
req.setConfiguration(manager.defaultConfiguration());

connect(&req, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));