123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #ifndef QTHTTPS_H
- #define QTHTTPS_H
- #include <QObject>
- #include <QNetworkReply>
- #include <QNetworkAccessManager>
- #include <QNetworkRequest>
- #include <QFile>
- #include <QTimer>
- class QtHttps : public QObject
- {
- Q_OBJECT
- public:
- explicit QtHttps(QObject *parent = nullptr);
- ~QtHttps();
- bool Get(const QString &url);
- bool GetHead(const QString &url);
- void Post(const QString &url, const QByteArray &data);
- void downloadFile(const QString &url, const QString &filePath);
- bool waitFinished(int msecs = 30000); /* 等待完成 */
- signals:
- void signal_replyFinished();
- void signal_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
- /* 判断是否是Qt5.15.2 */
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- void signal_error(QNetworkReply::NetworkError code);
- #endif
- private slots:
- void do_replyFinished();
- void do_replyReadyRead();
- void do_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- void do_error(QNetworkReply::NetworkError code);
- #endif
- void do_timeout();
- private:
- bool m_isFinished = false; /* 等待完成标志位 */
- QTimer m_timer; /* 超时定时器 */
- QNetworkAccessManager *m_manager = nullptr;
- QNetworkReply *m_reply = nullptr;
- QFile *m_file = nullptr; /* 保存临时文件 */
- };
- #endif /* QTHTTPS_H */
|