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??
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??
Have you tried the obvious - connect to error ( QNetworkReply::NetworkError ) signal of QNetworkReply object ?
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.
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.
Qt Code:
if(url.compare("your_url") == 0) { }To copy to clipboard, switch view to plain text mode
or
I already told you
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.
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.
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.
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:
#include <QtCore> #include <QtNetwork> #include <QDebug> { Q_OBJECT public: { } 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[]) { Fetcher f; return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
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??
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.
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.
Phlucious (8th July 2014)
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).
Phlucious (8th July 2014)
Bookmarks