Results 1 to 6 of 6

Thread: QNetworkReply error handling

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default QNetworkReply error handling

    I'd like to access a website using QNetworkReply and then extract the error from it. I'm most interested in seeing if the website was found or not. The below code always gives "Unknown Error" even when the website is perfectly fine. Has anyone done this before. I'm most thankful for your help.

    Qt Code:
    1. QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    2. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    3. QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("http://www.unknownhost123.com")));
    4. QString err = reply->errorString();//this err will be reported
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply error handling

    Does the actual code look exactly like you posted it here? QNetworkAccessManager works in an asynchronous way so the result is not yet ready when you call errorString() on the reply.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    timmu (25th August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkReply error handling

    Thanks Wysota!!
    For a very long time I've been spinning my wheels without having any idea what's going on. Now it seems that asynchronous is the keyword for me. However, I have to admit I don't know how to use this knowledge. I've been leraning from here http://doc.trolltech.com/4.4/qnetworkaccessmanager.html
    and I get things to work for one request but all falls apart when I have a while loop and try to do multiple queries. Would someone be able to do me a huge favor and perhaps show how this code could be changed to accept multiple requests.

    Here's my code:
    Qt Code:
    1. for(int y=1; y<10; y++)
    2. {
    3.  
    4. QNetworkAccessManager* nam = new QNetworkAccessManager(this);
    5. QNetworkReply* reply;
    6. fscanf(namefeed, "%s", &sona);
    7. urlstring=QString::fromLocal8Bit(sona, strlen(sona)); url.setUrl(urlstring);
    8. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    9. reply = nam->get(QNetworkRequest(url)); url.clear();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyClass::replyFinished(QNetworkReply* reply)
    2. {
    3. QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    4. if (statusCodeV.toInt()==200){
    5. //do something
    6. }
    7. else{
    8. // do something else
    9. }
    10.  
    11. delete reply;
    12. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply error handling

    You can use QEventLoop to turn the asynchronous mechanism into synchronous, if you want. An example of doing that is in the [wiki]Keeping the GUI Responsive[/wiki] article.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    timmu (25th August 2009)

  7. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkReply error handling

    This is a great idea! But would it work with my code above? Where would the QEventLoop go? I've been playing with other timing tricks but nothing so far. Also this didn't do it:
    Qt Code:
    1. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    Last edited by timmu; 25th August 2009 at 09:07.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply error handling

    Just read the article...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Finding filename from QNetworkReply
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2009, 09:29
  2. Custom QNetworkReply for WebView
    By victor.yacovlev in forum Qt Programming
    Replies: 0
    Last Post: 1st April 2009, 13:32
  3. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46

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.