#include #include #include #include #include #include #include #include #include #include #include #ifdef QT_NO_OPENSSL #error requires SSL #endif class DownloadItem : public QObject { Q_OBJECT public: DownloadItem(QNetworkReply* r); ~DownloadItem(); private slots: void readyRead(); void finished(); private: QNetworkReply* reply; //QFile file; }; class DownloadManager : public QObject { Q_OBJECT public: DownloadManager(); ~DownloadManager(); void get(const QUrl& url); private slots: void finished(QNetworkReply* reply); void authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator); void proxyAuthenticationRequired(const QNetworkProxy& proxy, QAuthenticator* authenticator); void sslErrors(QNetworkReply* reply, const QList& errors); private: QNetworkAccessManager nam; QList downloads; }; DownloadManager::DownloadManager() { connect(&nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*))); connect(&nam, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*))); connect(&nam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this, SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); connect(&nam, SIGNAL(sslErrors(QNetworkReply*, const QList&)), this, SLOT(sslErrors(QNetworkReply*, const QList&))); } DownloadManager::~DownloadManager() { } void DownloadManager::get(const QUrl& url) { QNetworkReply* reply = nam.get(QNetworkRequest(url)); downloads.append(new DownloadItem(reply)); } void DownloadManager::finished(QNetworkReply* reply) { qDebug() << "finished()"; QCoreApplication::quit(); } void DownloadManager::authenticationRequired(QNetworkReply* reply, QAuthenticator* auth) { qDebug() << "authenticationRequired"; auth->setUser("httptest"); auth->setPassword("httptest"); // todo } void DownloadManager::proxyAuthenticationRequired(const QNetworkProxy& proxy, QAuthenticator* auth) { qDebug() << "proxyAuthenticationRequired"; auth->setUser("qsockstest"); auth->setPassword("password"); //todo } void DownloadManager::sslErrors(QNetworkReply* reply, const QList& errors) { #ifndef QT_NO_SSL qDebug() << "sslErrors"; foreach(const QSslError& error, errors) qDebug() << error.errorString(); #endif } DownloadItem::DownloadItem(QNetworkReply* r) : reply(r) { reply->setParent(this); connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(reply, SIGNAL(finished()), this, SLOT(finished())); //todo, create file } DownloadItem::~DownloadItem() { } void DownloadItem::readyRead() { qDebug() << reply->readAll(); //todo, save } void DownloadItem::finished() { qDebug() << reply->error() << reply->errorString(); deleteLater(); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); //hardcoded proxy for test QNetworkProxy proxy(QNetworkProxy::HttpProxy, QLatin1String("qt-test-server.qt-test-net"), 3129); QNetworkProxy::setApplicationProxy(proxy); DownloadManager dl; qDebug() << app.arguments(); foreach(QString str, app.arguments().mid(1)) { dl.get(QUrl::fromUserInput(str)); } return app.exec(); } #include "qget.moc"