Timeout reply from QNetworkReply
-
In my code I have function to get request from http like below:
void NetworkWorker::get(QString location) { qInfo() << "Getting from server...."; QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 200 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); } connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); }
void NetworkWorker::readyRead() { QByteArray data; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); if (reply) { data = reply->readAll(); } networkReply_byRfid(data); }
How emit signal when is timeout, without any data to recive ? Is any build in mechanizm in QNetworkReply ?
-
You can set a timeout in your QNetworkRequest.
I expect you get a suitable code in the QNetworkReply::errorOccurred() signal if no data is received inside that timeout.If memory serves, the request is not sent until your code makes it to the Qt event loop. The way your code is written, you are checking for HTTP response codes before the GET has been sent.
-
@ChrisW67 said in Timeout reply from QNetworkReply:
If memory serves, the request is not sent until your code makes it to the Qt event loop. The way your code is written, you are checking for HTTP response codes before the GET has been sent.
I corrected it
-
Hi,
Use the finished signal of QNetworkReply.
-
Hi,
Use the finished signal of QNetworkReply.
@SGaist said in Timeout reply from QNetworkReply:
Hi,
Use the finished signal of QNetworkReply.
Can you clarify ?
-
Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.
-
Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.
-
@SGaist It may not always be the case. If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.
-
@JoeCFD said in Timeout reply from QNetworkReply:
If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.
?
-
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
-
@JoeCFD
Have you tested to see whether, with aconnect()
, thefinished
signal would still actually be received and trigger the slot (so no need to testisFinished()
)? My understanding is that is how slot connections work. -
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
@JoeCFD said in Timeout reply from QNetworkReply:
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
slot() --> it is my NetworkWorker::readyRead() ?
connect() --> what connect() are you talking about?
Still I do not know when I should emit signal about timeout recived data ....
-
@JoeCFD said in Timeout reply from QNetworkReply:
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
slot() --> it is my NetworkWorker::readyRead() ?
connect() --> what connect() are you talking about?
Still I do not know when I should emit signal about timeout recived data ....
@Damian7546 said in Timeout reply from QNetworkReply:
connect() --> what connect() are you talking about?
connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
slot() is your NetworkWorker::readyRead.
-
And where timeout is expired ?
I still understand where I can give for example :qDebug() << "timeout has expired to receive data"
void NetworkWorker::get(QString location) { QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { readyRead(); } else { connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); } } }
void NetworkWorker::readyRead() { QByteArray data; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 200 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); } else { if (reply) { data = reply->readAll(); } networkReply(data); } }
-
And where timeout is expired ?
I still understand where I can give for example :qDebug() << "timeout has expired to receive data"
void NetworkWorker::get(QString location) { QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { readyRead(); } else { connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); } } }
void NetworkWorker::readyRead() { QByteArray data; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 200 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); } else { if (reply) { data = reply->readAll(); } networkReply(data); } }
@Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.
-
@Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.
@jsulm Can you give me an example ?
After
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
I added:
qDebug() << "http error: " << reply->error();
And when I have connection to http server the shows message:
http error: QNetworkReply::NoError
But nothing shows up when there is no connection, because
readyread
doesn't appear.I should in this function check timeout:
void NetworkWorker::get(QString location) { qInfo() << "Getting from server...."; QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); }
So in abov funnction added :
connect(reply,&QNetworkReply::errorOccurred,this,&NetworkWorker::errorOccurred);
but never occurred....