QtHttps.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef QTHTTPS_H
  2. #define QTHTTPS_H
  3. #include <QObject>
  4. #include <QNetworkReply>
  5. #include <QNetworkAccessManager>
  6. #include <QNetworkRequest>
  7. #include <QFile>
  8. #include <QTimer>
  9. class QtHttps : public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit QtHttps(QObject *parent = nullptr);
  14. ~QtHttps();
  15. bool Get(const QString &url);
  16. bool GetHead(const QString &url);
  17. void Post(const QString &url, const QByteArray &data);
  18. void downloadFile(const QString &url, const QString &filePath);
  19. bool waitFinished(int msecs = 30000); /* 等待完成 */
  20. signals:
  21. void signal_replyFinished();
  22. void signal_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
  23. /* 判断是否是Qt5.15.2 */
  24. #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
  25. void signal_error(QNetworkReply::NetworkError code);
  26. #endif
  27. private slots:
  28. void do_replyFinished();
  29. void do_replyReadyRead();
  30. void do_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
  31. #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
  32. void do_error(QNetworkReply::NetworkError code);
  33. #endif
  34. void do_timeout();
  35. private:
  36. bool m_isFinished = false; /* 等待完成标志位 */
  37. QTimer m_timer; /* 超时定时器 */
  38. QNetworkAccessManager *m_manager = nullptr;
  39. QNetworkReply *m_reply = nullptr;
  40. QFile *m_file = nullptr; /* 保存临时文件 */
  41. };
  42. #endif /* QTHTTPS_H */