Results 1 to 9 of 9

Thread: QNetworkAccessManager timeout when server is down?

  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default QNetworkAccessManager timeout when server is down?

    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?

    Qt Code:
    1. void MyWidget::load() {
    2. QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this);
    3. connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(slotFinished(QNetworkReply*)));
    4. QUrl url("http://www.domain.com"); //A domain that will not respond and will timeout
    5. QNetworkReply *reply = networkAccessManager->get(QNetworkRequest(url));
    6. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(slotError( QNetworkReply::NetworkError)));
    7. }
    8.  
    9. void MyWidget::slotError(QNetworkReply::NetworkError) {
    10. ui.textEdit->setText("error");
    11. }
    12.  
    13. void MyWidget::slotFinished(QNetworkReply* reply) {
    14. ui.textEdit->setText("reply");
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: QNetworkAccessManager timeout when server is down?

    Anyone, is there a solution?

  3. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    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/2...anager-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.

  4. The following user says thank you to frankiefrank for this useful post:

    shorawitz (29th December 2011)

  5. #4
    Join Date
    Dec 2008
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    How do you cancel the operation?

    thanks in advance. Greg

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    Quote Originally Posted by barrygp View Post
    How do you cancel the operation?
    By calling QNetworkReply::abort()?

    Cheers,
    _

  7. #6
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    Quote Originally Posted by anda_skoa View Post
    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?

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    Quote Originally Posted by sattu View Post
    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."

    Quote Originally Posted by sattu View Post
    And also, after QNetworkReply::abort(), will I need to call deleteLater() method also?
    Of course.

    Cheers,
    _

  9. #8
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    Quote Originally Posted by anda_skoa View Post
    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-
    Qt Code:
    1. void MyApp::monitorWebServerTimeout()
    2. {
    3. m_reply->abort();
    4. m_reply->deleteLater();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Second-
    Qt Code:
    1. void MyApp::monitorWebServerTimeout()
    2. {
    3. m_networkManager->deleteResource(m_request);
    4. m_reply->abort();
    5. m_reply->deleteLater();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager timeout when server is down?

    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,
    _

Similar Threads

  1. QHttp Timeout?
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 25th February 2010, 10:43
  2. QBasicTimer never timeout.
    By kunalnandi in forum Qt Programming
    Replies: 5
    Last Post: 30th January 2010, 08:55
  3. qextserialport waitForReadyRead and timeOut
    By adamatic in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2009, 10:52
  4. Tootip Timeout
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2008, 11:17
  5. QTcpSocket + timeout
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2008, 14:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.