PDA

View Full Version : How to find the error in QNetworkReply?



Gokulnathvc
6th April 2012, 09:37
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??

stampede
6th April 2012, 11:27
Have you tried the obvious - connect to error ( QNetworkReply::NetworkError ) signal of QNetworkReply object ?

ChrisW67
6th April 2012, 11:30
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.

Gokulnathvc
10th April 2012, 07:44
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.

zgulser
10th April 2012, 12:14
QString url = reply->url().toString();

if(url.compare("your_url") == 0)
{

}


or



if(url.isValid())
{

}

ChrisW67
11th April 2012, 03:04
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


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.

Gokulnathvc
12th April 2012, 14:00
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.

Talei
12th April 2012, 16:25
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.

ChrisW67
13th April 2012, 01:54
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.



#include <QtCore>
#include <QtNetwork>
#include <QDebug>

class Fetcher: public QObject
{
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p)
{
QTimer::singleShot(0, this, SLOT(start()));
}

public slots:
void start() {
// QNetworkRequest req(QUrl("http://www.google.com/")); // works, no error, but generally HTTP 302 redirects outside the US
QNetworkRequest req(QUrl("http://www.google.com/doesnotexist.gif")); // doesn't work, error 203 and HTTP status 404
// QNetworkRequest req(QUrl("ftp://ftp.qt.nokia.com/robots.txt")); // works, no error, no HTTP status
// QNetworkRequest req(QUrl("ftp://ftp.qt.nokia.com/doesnotexist.txt")); // doesn't work, error 203, no HTTP status
reply = nam.get(req);

connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(error(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}

void error(QNetworkReply::NetworkError code) {
qDebug() << "QNetworkReply::NetworkError " << code << "received";
}

void replyFinished() {
qDebug() << "QNetworkRequest::HttpStatusCodeAttribute" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ) << "received";
qDebug() << "Reply contains" << reply->bytesAvailable() << "bytes";
qDebug() << "Finished";

qApp->quit();
}

private:
QNetworkAccessManager nam;
QNetworkReply *reply;
};

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Fetcher f;
return app.exec();
}
#include "main.moc"

Gokulnathvc
20th April 2012, 07:52
I have used QVariant statusphrase = g_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribu te); in done() slot.. it always says ok if the file in the url doesnt exists.. How to fix this??

ChrisW67
20th April 2012, 09:04
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.

Gokulnathvc
20th April 2012, 09:42
http://wordpress.org/support/topic/123.gif

wysota
20th April 2012, 11:08
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.

ChrisW67
20th April 2012, 11:40
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).