123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #include "QtHttps.h"
- #include "LightLog.h"
- #include <QEventLoop>
- QtHttps::QtHttps(QObject *parent) :
- QObject(parent)
- {
-
- m_manager = new QNetworkAccessManager(this);
-
- m_timer.setTimerType(Qt::PreciseTimer);
- m_timer.setSingleShot(true);
-
- bool isSupport = QSslSocket::supportsSsl();
- QString opensslVersion = QSslSocket::sslLibraryVersionString();
- QString openSSLBuildVersion = QSslSocket::sslLibraryBuildVersionString();
- QLOG_INFO("OpenSSL is support : " + QString::number(isSupport));
-
-
-
- auto list = m_manager->supportedSchemes();
- QString str;
- for(auto &item : list)
- {
- str = str + item + " ";
- }
- QLOG_INFO("supportedSchemes : " + str);
-
- connect(&m_timer, &QTimer::timeout, this, &QtHttps::do_timeout);
- }
- QtHttps::~QtHttps()
- {
- delete m_manager;
- if(m_file != nullptr)
- {
- if(m_file->isOpen())
- {
- m_file->close();
- }
- delete m_file;
- m_file = nullptr;
- }
- if(m_reply != nullptr)
- {
- m_reply->deleteLater();
- m_reply = nullptr;
- }
- }
- bool QtHttps::Get(const QString &url)
- {
- if(m_manager == nullptr)
- {
- QLOG_WARN("m_manager is nullptr");
- return false;
- }
- QNetworkRequest request;
- request.setUrl(QUrl(url));
-
-
- m_reply = m_manager->get(request);
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::do_replyFinished);
- connect(m_reply, &QNetworkReply::readyRead, this, &QtHttps::do_replyReadyRead);
- connect(m_reply, &QNetworkReply::downloadProgress, this, &QtHttps::do_downloadProgress);
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- connect(m_reply, &QNetworkReply::errorOccurred, this, &QtHttps::do_error);
- #endif
-
- connect(m_reply, &QNetworkReply::downloadProgress, this, &QtHttps::signal_downloadProgress);
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::signal_replyFinished);
- m_isFinished = false;
- return true;
- }
- bool QtHttps::GetHead(const QString &url)
- {
- if(m_manager == nullptr)
- {
- QLOG_WARN("m_manager is nullptr");
- return false;
- }
- QNetworkRequest request;
-
-
-
-
- request.setUrl(url);
- m_reply = m_manager->head(request);
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::do_replyFinished);
- connect(m_reply, &QNetworkReply::readyRead, this, &QtHttps::do_replyReadyRead);
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- connect(m_reply, &QNetworkReply::errorOccurred, this, &QtHttps::do_error);
- #endif
-
- connect(m_reply, &QNetworkReply::downloadProgress, this, &QtHttps::signal_downloadProgress);
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::signal_replyFinished);
- m_isFinished = false;
- return true;
- }
- void QtHttps::Post(const QString &url, const QByteArray &data)
- {
- }
- void QtHttps::downloadFile(const QString &url, const QString &filePath)
- {
- if(m_manager == nullptr)
- {
- QLOG_WARN("m_manager is nullptr");
- return;
- }
-
- m_file = new QFile(this);
- m_file->setFileName(filePath);
- if(!m_file->open(QIODevice::WriteOnly | QIODevice::Truncate))
- {
- QLOG_WARN("open file failed , file path : " + filePath);
- m_file->close();
- delete m_file;
- m_file = nullptr;
- return;
- }
-
- QNetworkRequest request;
- request.setUrl(QUrl(url));
- m_reply = m_manager->get(request);
-
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::do_replyFinished);
- connect(m_reply, &QNetworkReply::readyRead, this, &QtHttps::do_replyReadyRead);
- connect(m_reply, &QNetworkReply::downloadProgress, this, &QtHttps::do_downloadProgress);
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- connect(m_reply, &QNetworkReply::errorOccurred, this, &QtHttps::do_error);
- #endif
-
- connect(m_reply, &QNetworkReply::downloadProgress, this, &QtHttps::signal_downloadProgress);
- connect(m_reply, &QNetworkReply::finished, this, &QtHttps::signal_replyFinished);
- m_isFinished = false;
- }
- bool QtHttps::waitFinished(int msecs)
- {
- if(msecs == 0)
- {
- return m_isFinished;
- }
- else if(msecs == -1)
- {
- QEventLoop loop;
- connect(this, &QtHttps::signal_replyFinished, &loop, &QEventLoop::quit);
- loop.exec();
- return true;
- }
- else if (msecs > 0)
- {
- m_timer.start(msecs);
- return m_isFinished;
- }
- return m_isFinished;
- }
- void QtHttps::do_replyFinished()
- {
- QLOG_DEBUG("do_replyFinished");
-
- if(m_timer.isActive())
- {
- m_timer.stop();
- }
-
- auto retCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- if(m_reply->error() == QNetworkReply::NoError)
- {
- QLOG_DEBUG("reply : " + QString::number(retCode));
- auto list = m_reply->rawHeaderList();
- for(auto &item : list)
- {
-
- QLOG_DEBUG(item + " : " + m_reply->rawHeader(item));
- }
-
- if(m_file != nullptr)
- {
- if(m_file->isOpen())
- {
- m_file->close();
- }
- delete m_file;
- m_file = nullptr;
- }
- }
- else
- {
- QLOG_WARN("reply error : " + QString::number(retCode) + " " + m_reply->errorString());
- }
-
- if(m_file != nullptr)
- {
- if(m_file->isOpen())
- {
- m_file->close();
- }
- delete m_file;
- m_file = nullptr;
- }
-
- m_reply->deleteLater();
- m_reply = nullptr;
-
- m_isFinished = true;
- }
- void QtHttps::do_replyReadyRead()
- {
-
- if(m_file != nullptr)
- {
- m_file->write(m_reply->readAll());
- }
- }
- void QtHttps::do_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
- {
-
- QLOG_DEBUG("bytesReceived / bytesTotal : " + QString::number(bytesReceived) + " / " + QString::number(bytesTotal));
- }
- #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- void QtHttps::do_error(QNetworkReply::NetworkError code)
- {
- QLOG_WARN("error : " + QString::number(code));
- }
- #endif
- void QtHttps::do_timeout()
- {
- QLOG_WARN("Http timeout");
- if(m_reply != nullptr)
- {
- m_reply->disconnect();
- m_reply->abort();
- m_reply->deleteLater();
- m_reply = nullptr;
- }
- m_isFinished = true;
- }
|