Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef DOWNLOADER_H
- #define DOWNLOADER_H
- #include <QObject>
- #include <QNetworkAccessManager>
- #include <QNetworkRequest>
- #include <QNetworkReply>
- #include <QUrl>
- #include <QDateTime>
- #include <QFile>
- #include <QDebug>
- class Downloader : public QObject
- {
- Q_OBJECT
- public:
- explicit Downloader(QObject *parent = 0);
- void doDownload();
- signals:
- void finished() const;
- public slots:
- void replyOk(QNetworkReply *reply);
- private:
- QNetworkAccessManager *manager;
- };
- #endif // DOWNLOADER_H
- --------------------------
- #include "Downloader.h"
- Downloader::Downloader(QObject *parent) :
- QObject(parent)
- {
- }
- void Downloader::doDownload()
- {
- manager = new QNetworkAccessManager(this);
- connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyOk(QNetworkReply*)));
- manager->get(QNetworkRequest(QUrl("https://www.google.com/killer-robots.txt")));
- }
- void Downloader::replyOk (QNetworkReply *reply)
- {
- if(reply->error())
- {
- qDebug() << "ERROR!";
- qDebug() << reply->errorString();
- }
- else
- {
- qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
- qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
- qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
- qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
- QFile *file = new QFile("C:\\downloaded.txt");
- if(file->open(QFile::Append))
- {
- file->write(reply->readAll());
- file->flush();
- file->close();
- }
- delete file;
- }
- reply->deleteLater();
- emit finished();
- }
- --------------------------------------
- -MainDownload.cpp-
- #include Downloader.h
- MainDownload::MainDownload()
- {
- }
- void MainDownload::ExecuteDownload()
- {
- Downloader *D = new Downloader();
- D->doDownload();
- connect(D, &Downloader::finished, D, &Downloader::deleteLater);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement