Advertisement
Guest User

Untitled

a guest
Jan 18th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #ifndef DOWNLOADER_H
  2. #define DOWNLOADER_H
  3.  
  4. #include <QObject>
  5. #include <QNetworkAccessManager>
  6. #include <QNetworkRequest>
  7. #include <QNetworkReply>
  8. #include <QUrl>
  9. #include <QDateTime>
  10. #include <QFile>
  11. #include <QDebug>
  12.  
  13. class Downloader : public QObject
  14. {
  15. Q_OBJECT
  16. public:
  17. explicit Downloader(QObject *parent = 0);
  18. void doDownload();
  19.  
  20. signals:
  21. void finished() const;
  22.  
  23. public slots:
  24. void replyOk(QNetworkReply *reply);
  25.  
  26. private:
  27. QNetworkAccessManager *manager;
  28.  
  29. };
  30.  
  31. #endif // DOWNLOADER_H
  32.  
  33. --------------------------
  34.  
  35. #include "Downloader.h"
  36.  
  37. Downloader::Downloader(QObject *parent) :
  38. QObject(parent)
  39. {
  40. }
  41.  
  42. void Downloader::doDownload()
  43. {
  44. manager = new QNetworkAccessManager(this);
  45.  
  46. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyOk(QNetworkReply*)));
  47.  
  48. manager->get(QNetworkRequest(QUrl("https://www.google.com/killer-robots.txt")));
  49. }
  50.  
  51. void Downloader::replyOk (QNetworkReply *reply)
  52. {
  53.  
  54. if(reply->error())
  55. {
  56. qDebug() << "ERROR!";
  57. qDebug() << reply->errorString();
  58. }
  59. else
  60. {
  61. qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
  62. qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
  63. qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
  64. qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  65. qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
  66.  
  67. QFile *file = new QFile("C:\\downloaded.txt");
  68. if(file->open(QFile::Append))
  69. {
  70. file->write(reply->readAll());
  71. file->flush();
  72. file->close();
  73. }
  74. delete file;
  75. }
  76. reply->deleteLater();
  77.  
  78. emit finished();
  79. }
  80.  
  81. --------------------------------------
  82.  
  83. -MainDownload.cpp-
  84.  
  85. #include Downloader.h
  86.  
  87. MainDownload::MainDownload()
  88. {
  89. }
  90.  
  91. void MainDownload::ExecuteDownload()
  92. {
  93. Downloader *D = new Downloader();
  94. D->doDownload();
  95. connect(D, &Downloader::finished, D, &Downloader::deleteLater);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement