network request will always timeout with a long time server return?
-
first of all ,sorry for my bad english..
if server takes a long time to return,more than 300 seconds,QT request will timeout,reply will abort.
for eg.
php server code.server will return after 6*60 seconds .<?php sleep(60*6); echo "test"; die(); ?>
qt codes
#include <QCoreApplication> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkRequest> #include <QDebug> #include <QObject> #include <QEventLoop> #include <QDateTime> void replyFinished(){ QDateTime dateTime; qDebug() << "replyFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); } void managerFinished(){ QDateTime dateTime; qDebug() << "managerFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager manager; manager.setTransferTimeout(60*1000 * 7);// 7 minutes timeout QNetworkReply *reply; QNetworkRequest request; request.setTransferTimeout(60*1000 * 7);// 7 minutes timeout request.setUrl(QUrl(u8"https://www.dajuan.com/home/test"));// 6 minutes return "test" request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::NoLessSafeRedirectPolicy); reply = manager.get(request); QObject::connect(reply,&QNetworkReply::finished,&a,replyFinished); QDateTime dateTime; qDebug() << "reuqest begin " << dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); QEventLoop eventLoop; //更新进度条 QObject::connect(&manager,&QNetworkAccessManager::finished,&eventLoop,&QEventLoop::quit); QObject::connect(&manager,&QNetworkAccessManager::finished,&a,managerFinished); //block until finish eventLoop.exec(); qDebug() << "reuqest done "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); if(reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); QString s = QString::fromStdString(data.toStdString()); qDebug() << "reply string " << s; } else{ QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QString msg = QString("code:%1 \ninfo:%2").arg(statusCode.toInt()).arg(reply->errorString()); qDebug() << "reply error string " << msg; } return a.exec(); }
debug output is like this,we can see that the request is [timeout,after 7 miniutes]
reuqest begin "2024-06-25 09:32:15" managerFinished "2024-06-25 09:39:15" replyFinished "2024-06-25 09:39:15" reuqest done "2024-06-25 09:39:15" reply error string "code:0 \ninfo:Operation canceled"
if server return time is shorter than 300s ,debug output is down here ..I performs well.
reuqest begin "2024-06-25 09:42:48" managerFinished "2024-06-25 09:43:49" replyFinished "2024-06-25 09:43:49" reuqest done "2024-06-25 09:43:49" reply string "test"
I know that ,QNetworkAccessManager and QNetworkRequest default timeout is 300s.(Also,I don't konw the difference of these two timeout...)
my qt version is 5.15.2 ,and 5.15.14(i thought it's a bug,so I upgrades to 5.15.14..but, these 2 version is the same result)So,this is a bug,or I used this in a wrong way?
-
first of all ,sorry for my bad english..
if server takes a long time to return,more than 300 seconds,QT request will timeout,reply will abort.
for eg.
php server code.server will return after 6*60 seconds .<?php sleep(60*6); echo "test"; die(); ?>
qt codes
#include <QCoreApplication> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkRequest> #include <QDebug> #include <QObject> #include <QEventLoop> #include <QDateTime> void replyFinished(){ QDateTime dateTime; qDebug() << "replyFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); } void managerFinished(){ QDateTime dateTime; qDebug() << "managerFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager manager; manager.setTransferTimeout(60*1000 * 7);// 7 minutes timeout QNetworkReply *reply; QNetworkRequest request; request.setTransferTimeout(60*1000 * 7);// 7 minutes timeout request.setUrl(QUrl(u8"https://www.dajuan.com/home/test"));// 6 minutes return "test" request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::NoLessSafeRedirectPolicy); reply = manager.get(request); QObject::connect(reply,&QNetworkReply::finished,&a,replyFinished); QDateTime dateTime; qDebug() << "reuqest begin " << dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); QEventLoop eventLoop; //更新进度条 QObject::connect(&manager,&QNetworkAccessManager::finished,&eventLoop,&QEventLoop::quit); QObject::connect(&manager,&QNetworkAccessManager::finished,&a,managerFinished); //block until finish eventLoop.exec(); qDebug() << "reuqest done "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); if(reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); QString s = QString::fromStdString(data.toStdString()); qDebug() << "reply string " << s; } else{ QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QString msg = QString("code:%1 \ninfo:%2").arg(statusCode.toInt()).arg(reply->errorString()); qDebug() << "reply error string " << msg; } return a.exec(); }
debug output is like this,we can see that the request is [timeout,after 7 miniutes]
reuqest begin "2024-06-25 09:32:15" managerFinished "2024-06-25 09:39:15" replyFinished "2024-06-25 09:39:15" reuqest done "2024-06-25 09:39:15" reply error string "code:0 \ninfo:Operation canceled"
if server return time is shorter than 300s ,debug output is down here ..I performs well.
reuqest begin "2024-06-25 09:42:48" managerFinished "2024-06-25 09:43:49" replyFinished "2024-06-25 09:43:49" reuqest done "2024-06-25 09:43:49" reply string "test"
I know that ,QNetworkAccessManager and QNetworkRequest default timeout is 300s.(Also,I don't konw the difference of these two timeout...)
my qt version is 5.15.2 ,and 5.15.14(i thought it's a bug,so I upgrades to 5.15.14..but, these 2 version is the same result)So,this is a bug,or I used this in a wrong way?
@nelson-W said in network request will always timeout with a long time server return?:
QEventLoop eventLoop;
Why a local event loop?
-
@nelson-W said in network request will always timeout with a long time server return?:
QEventLoop eventLoop;
Why a local event loop?
-
@nelson-W said in network request will always timeout with a long time server return?:
Synchronous request.
But do you really need synchronous requests?