Results 1 to 14 of 14

Thread: How to find the error in QNetworkReply?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to find the error in QNetworkReply?

    I have used QNetworkReply to download the file from Http. If i specify the wrong url also, the download starts and give some garbage values. How to find whether the url exists or not in QNetworkReply and handle accordingly??

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to find the error in QNetworkReply?

    Have you tried the obvious - connect to error ( QNetworkReply::NetworkError ) signal of QNetworkReply object ?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find the error in QNetworkReply?

    Connect a slot to your QNetworkReply::error() signal and you will be advised of network and other errors. If you are connecting to a server but requesting a page that does not exist then you will receive whatever the server sends as am HTTP 404 error message as the content of the stream.

  4. #4
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to find the error in QNetworkReply?

    I just want to check whether the url passed is correct or not. If a particular location of the file in Url does exists or not.

  5. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to find the error in QNetworkReply?

    Qt Code:
    1. QString url = reply->url().toString();
    2.  
    3. if(url.compare("your_url") == 0)
    4. {
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. if(url.isValid())
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find the error in QNetworkReply?

    Quote Originally Posted by Gokulnathvc View Post
    I just want to check whether the url passed is correct or not. If a particular location of the file in Url does exists or not.
    I already told you

    Quote Originally Posted by ChrisW67 View Post
    Connect a slot to your QNetworkReply::error() signal and you will be advised of network and other errors. If you are connecting to a server but requesting a page that does not exist then you will receive whatever the server sends as am HTTP 404 error message as the content of the stream.
    If the file URL does not match a valid target at the server then you will receive an error. If you look at the error you will know. You look at the error by connecting to the QNetworkReply::error() signal. When you see QNetworkReply::ContentNotFoundError you know the host/port part of the URL was good but that the path did not identify a valid location on that host. This is as close to "file in the Url does exists or not" as you are going to get given that a good URL may not point at a file of any sort any way.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to find the error in QNetworkReply?

    I supplied wrong url to the QUrl, but QNetworkReply::error() doesnt show any error. i gave the url where the particular file is not found. But i didnt get any error message.

  8. #8
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to find the error in QNetworkReply?

    I think that error is with You parsing first reply. There are sites that use no status code but HTML page to report if file exist or not.

    enum QNetworkReply::NetworkError is more protocol TCP oriented / connection itself oriented. Reported errors are like proxy auth., host not found, etc.
    But what You are probably looking for is enum QNetworkRequest::Attribute. To be more precise QNetworkRequest::HttpStatusCodeAttribute. This will tell you if server return 200 = OK, 404 = not found, 302 = redirection etc. Keep in mind that if server reply valid HTML page with "Sorry file is not on this server" without 404 then You need to parse HTML.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find the error in QNetworkReply?

    If the server returns an HTTP 404 status then either method will work (error == 203, see example below). If the server does not return a 404 status, for example sending a 302 redirect instead, then you do not get an error because you should not. If the URL is an FTP url then you will not receive HTTP status codes at all. Gokulnathvc needs to know what the servers return for the requests being made, and also needs to understand that "the url exists" can mean a range of things.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class Fetcher: public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. Fetcher(QObject *p = 0): QObject(p)
    10. {
    11. QTimer::singleShot(0, this, SLOT(start()));
    12. }
    13.  
    14. public slots:
    15. void start() {
    16. // QNetworkRequest req(QUrl("http://www.google.com/")); // works, no error, but generally HTTP 302 redirects outside the US
    17. QNetworkRequest req(QUrl("http://www.google.com/doesnotexist.gif")); // doesn't work, error 203 and HTTP status 404
    18. // QNetworkRequest req(QUrl("ftp://ftp.qt.nokia.com/robots.txt")); // works, no error, no HTTP status
    19. // QNetworkRequest req(QUrl("ftp://ftp.qt.nokia.com/doesnotexist.txt")); // doesn't work, error 203, no HTTP status
    20. reply = nam.get(req);
    21.  
    22. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(error(QNetworkReply::NetworkError)));
    23. connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    24. }
    25.  
    26. void error(QNetworkReply::NetworkError code) {
    27. qDebug() << "QNetworkReply::NetworkError " << code << "received";
    28. }
    29.  
    30. void replyFinished() {
    31. qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) << "received";
    32. qDebug() << "Reply contains" << reply->bytesAvailable() << "bytes";
    33. qDebug() << "Finished";
    34.  
    35. qApp->quit();
    36. }
    37.  
    38. private:
    39. QNetworkAccessManager nam;
    40. QNetworkReply *reply;
    41. };
    42.  
    43. int main(int argc, char *argv[])
    44. {
    45. QCoreApplication app(argc, argv);
    46. Fetcher f;
    47. return app.exec();
    48. }
    49. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to find the error in QNetworkReply?

    I have used QVariant statusphrase = g_reply->attribute(QNetworkRequest::HttpReasonPhraseAttrib ute); in done() slot.. it always says ok if the file in the url doesnt exists.. How to fix this??

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find the error in QNetworkReply?

    You don't "fix this": this is exactly what the server sent. Human readable messages are not reliable anyway, which is why there are status codes.

    You have a complete working example in the post before yours. Point it at your URL and tell us what the result is. If the URL is public then post the URL here as well.
    Last edited by ChrisW67; 20th April 2012 at 08:12.

  12. #12
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to find the error in QNetworkReply?


  13. #13
    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: How to find the error in QNetworkReply?

    The server reports status 302 for this URL redirecting to http://wordpress.org/support/topic/. In other words as far as the server is concerned, the URL exists.
    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.


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

    Phlucious (8th July 2014)

  15. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find the error in QNetworkReply?

    To further emphasise: there is a difference between a URL and a file. Many URLs ultimately map directly to a file, but they may redirect elsewhere, be summarily ignored (e.g. if no session cookie present), or a response can be generated on the fly. In this case the server has converted the target path "/topic/123.gif" to a redirect (which it tells you about with status code 302), and the target of the redirection is an HTML document that tells a human viewer that the requested URL does not exist (which does exist resulting in a 200 status code).

  16. The following user says thank you to ChrisW67 for this useful post:

    Phlucious (8th July 2014)

Similar Threads

  1. SOLVED: error: QNetworkReply: No such file or directory
    By TheIndependentAquarius in forum Qt Programming
    Replies: 0
    Last Post: 25th May 2011, 09:33
  2. cannot find -lqwtd error
    By mobucl in forum Qwt
    Replies: 5
    Last Post: 6th May 2011, 12:59
  3. QNetworkReply error handling
    By timmu in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2009, 09:07
  4. error: cannot find -lovlnxlib
    By Tondog in forum KDE Forum
    Replies: 0
    Last Post: 14th August 2009, 15:20
  5. Find one error
    By BadKnees in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 15:33

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.