#include <QtCore>
#include <QtNetwork>
#include <QDebug>
{
Q_OBJECT
public:
{
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[])
{
Fetcher f;
return app.exec();
}
#include "main.moc"
#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"
To copy to clipboard, switch view to plain text mode
Bookmarks