PDA

View Full Version : QNetworkAccessManager timeout when server is down?



martinn
18th March 2010, 17:56
I'm trying to load some data with QNetworkAccessManager. The problem is that if my server doesn't respond, is down or something the QNetworkAccessManager::finished() signal never emit neither does the QNetworkReply::error(QNetworkReply::NetworkError). Can I set some kind of timeout so if the server doesn't responed I get an error? I haven't find a way to se the timeout, where can I do that? Or am I doing something wrong?



void MyWidget::load() {
QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this);
connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(slotFinished(QNetworkReply*)));
QUrl url("http://www.domain.com"); //A domain that will not respond and will timeout
QNetworkReply *reply = networkAccessManager->get(QNetworkRequest(url));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SL OT(slotError( QNetworkReply::NetworkError)));
}

void MyWidget::slotError(QNetworkReply::NetworkError) {
ui.textEdit->setText("error");
}

void MyWidget::slotFinished(QNetworkReply* reply) {
ui.textEdit->setText("reply");
}

martinn
19th March 2010, 13:31
Anyone, is there a solution?

frankiefrank
16th May 2011, 12:55
Hi Martinn. Sure you've already passed the timeout of this issue :) BUT I've been looking for a solution to the same thing, and looks like the way to go is by implementing a timer and using the "downloadProgress" signal emitted from the reply to know that your download is still happening.

See this post in Stack Overflow:
http://stackoverflow.com/questions/2668519/qnetworkaccessmanager-timeout

Kaleb Pederson's answer:
Use the QNetworkReply::uploadProgress() (or downloadProgress) signal to alert you that the operation is progressing. Then, set a timer for <n> seconds after the last uploadProgress/downloadProgress notification (with the timer started when the download/upload commenced.) If the download ever stops, you can cancel the operation <n> seconds after the last update.

barrygp
6th January 2014, 18:54
How do you cancel the operation?

thanks in advance. Greg

anda_skoa
6th January 2014, 23:35
How do you cancel the operation?

By calling QNetworkReply::abort()?

Cheers,
_

sattu
9th January 2014, 10:31
By calling QNetworkReply::abort()?
_
Hei hi, I had a doubt. Like QNetworkReply::abort() there is another function QNetworkAccessManager::deleteResource(const QNetworkRequest & request). Can you tell me when to use it? Since my question is same, i.e
how to cancel a server operation when the operation times out, so I didn't ask in a separate thread. And also, after QNetworkReply::abort(), will I need to call deleteLater() method also?

anda_skoa
9th January 2014, 13:13
Hei hi, I had a doubt. Like QNetworkReply::abort() there is another function QNetworkAccessManager::deleteResource(const QNetworkRequest & request). Can you tell me when to use it?


It is generally a good idea to read the documentation.
In tihs case it would have told you that deleteResource "Sends a request to delete the resource identified by the URL of request."



And also, after QNetworkReply::abort(), will I need to call deleteLater() method also?

Of course.

Cheers,
_

sattu
9th January 2014, 13:28
It is generally a good idea to read the documentation.
In tihs case it would have told you that deleteResource "Sends a request to delete the resource identified by the URL of request."
_
I know, I have gone through it, but I didn't understand where exactly to use in my code. I tried searching for some Qt examples in internet wherein this function could be used, but I didn't get any, so asked here. To be more precise, what could be the difference between my following 2 timeout functions-
First-


void MyApp::monitorWebServerTimeout()
{
m_reply->abort();
m_reply->deleteLater();
}


Second-


void MyApp::monitorWebServerTimeout()
{
m_networkManager->deleteResource(m_request);
m_reply->abort();
m_reply->deleteLater();
}


I implemented them in my code but couldn't exactly understand the difference between the 2.

anda_skoa
9th January 2014, 16:32
The first aborts a network access manager request and then schedules the deletion of the reply object.

The second starts a new request, a resource deletion, and then aborts a stored request and schedules its deletion.

Cheers,
_