Results 1 to 4 of 4

Thread: How to retry network request for specified amount of time

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to retry network request for specified amount of time

    I want to do Http Post and retry if in case some error has occured.

    Qt Code:
    1. HttpEngine::HttpEngine()
    2. {
    3. nwAccMan = new QNetworkAccessManager(this);
    4. connect(nwAccMan, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    5. QNetworkProxy proxy(QNetworkProxy::HttpProxy, "192.168.1.111", 810, QString("TTR"), QString());
    6. nwAccMan->setProxy(proxy);
    7. nwAccMan->post(QNetworkRequest(QUrl("https://something.com/fll/browseFolder?")), QByteArray("uid=test2_sdaee169&pwd=pass&p=%2f%2f"));
    8. }
    9.  
    10. void HttpEngine::replyFinished(QNetworkReply * reply)
    11. {
    12. QByteArray data = reply->readAll();
    13. qDebug() << data;
    14. }
    To copy to clipboard, switch view to plain text mode 

    In the above code, if in case the reply has some error, I need to try to resend request till a timer timeout with interval of 5 seconds. How can I achieve this ?

    Thank you.

  2. #2
    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: How to retry network request for specified amount of time

    Create a QTimer and connect it to a slot.
    On the first try of the request you start it. If it its slot gets called you cancel the current request and abort. If replyFinished() reports success you stop the timer.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    rawfool (20th August 2014)

  4. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to retry network request for specified amount of time

    Thank you anda_skoa.

    I did like this, found it working fine. But just wanted to check with you, if I'm doing it the correct way.
    And in replyFinished() slot, I am posting the network request again, if theres is any error and also if the timer is still active. Is it correct ?

    Qt Code:
    1. HttpEngine::HttpEngine()
    2. {
    3. timer.setInterval(25000);
    4. connect(&timer, SIGNAL(timeout()), &timer, SLOT(stop()));
    5.  
    6. nwAccMan = new QNetworkAccessManager(this);
    7. connect(nwAccMan, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    8. QNetworkProxy proxy(QNetworkProxy::HttpProxy, "192.168.1.111", 810, QString("TTR"), QString());
    9. nwAccMan->setProxy(proxy);
    10. nwAccMan->post(QNetworkRequest(QUrl("https://something.com/fll/browseFolder?")), QByteArray("uid=test2_sdaee169&pwd=pass&p=%2f%2f"));
    11. timer.start();
    12. }
    13.  
    14. void HttpEngine::replyFinished(QNetworkReply * reply)
    15. {
    16. qDebug() << "Inside replyFinished()";
    17. if(reply->error() == QNetworkReply::NoError)
    18. {
    19. if(timer.isActive())
    20. {
    21. qDebug() << "Timer is active";
    22. timer.stop();
    23. }
    24.  
    25. QByteArray data = reply->readAll();
    26. qDebug() << data;
    27. }
    28. else
    29. {
    30. if(timer.isActive())
    31. {
    32. qDebug() << "Timer is active";
    33. qDebug() << "Error: " << reply->errorString();
    34. nwAccMan->post(QNetworkRequest(QUrl("https://something.com/fll/browseFolder?")), QByteArray("uid=test2_sdaee169&pwd=pass&p=%2f%2f"));
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    Thank you.
    Last edited by rawfool; 20th August 2014 at 09:51.

  5. #4
    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: How to retry network request for specified amount of time

    Yes, that looks about right.

    Cheers,
    _

Similar Threads

  1. QaudioOutput play buffer audio real time disconnect network ?
    By Thành Viên Mới in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2011, 13:20
  2. network request more than one
    By mero in forum Qt Programming
    Replies: 11
    Last Post: 11th March 2011, 21:48
  3. Replies: 0
    Last Post: 25th April 2010, 16:50
  4. Refreshing program state after a certain amount of time
    By kremuwa in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2010, 15:30
  5. QSqlDatabase Time out no network to long....
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2007, 01:41

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.