qt Https QNetWorkRequest 配置 ssl ,实现访问调用 https 接口
SslConfiguationTool 类
#ifndef SSLCONFIGUATIONTOOL_H
#define SSLCONFIGUATIONTOOL_H
#include <QWebSocket>
class SslConfiguationTool
{
public:
static void setSslConfiguation(QWebSocket &webSocket);
static void setSslConfiguation(QNetworkRequest &request);
};
#endif // SSLCONFIGUATIONTOOL_H
#include "SslConfiguationTool.h"
void SslConfiguationTool::setSslConfiguation(QWebSocket &webSocket)
{
QSslConfiguration sslConf = webSocket.sslConfiguration();
sslConf.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConf.setProtocol(QSsl::TlsV1SslV3);
webSocket.setSslConfiguration(sslConf);
}
void SslConfiguationTool::setSslConfiguation(QNetworkRequest &request)
{
QSslConfiguration sslConf = request.sslConfiguration();
sslConf.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConf.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(sslConf);
}
实现调用http https 接口, 只实现了QNetWorkRquest get 函数
#ifndef HTTPREQUESTMANAGER_H
#define HTTPREQUESTMANAGER_H
#include <QObject>
#include <QMutex>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMetaEnum>
/**
* @brief Http请求管理类
*/
class HttpRequestManager : public QObject
{
Q_OBJECT
public:
static HttpRequestManager *getInstance();
/**
* @brief get
* @param url 如果启用ssl http 改为 https
*/
void get(const QString &url);
private slots:
void onTimeOut();
void onHttpGetFinished();
void onHttpGetError(QNetworkReply::NetworkError code);
void onHttpGetSslError(const QList<QSslError> &errors);
private:
explicit HttpRequestManager(QObject *parent = nullptr);
private:
static HttpRequestManager *instance;
static QMutex m_mutex;
QNetworkAccessManager *m_networkManager = nullptr;
QNetworkReply *m_reply = nullptr;
QTimer *m_timer = nullptr;
};
#endif // HTTPREQUESTMANAGER_H
#include "HttpRequestManager.h"
#include "SslConfiguationTool.h"
HttpRequestManager *HttpRequestManager::instance = nullptr;
QMutex HttpRequestManager::m_mutex;
HttpRequestManager *HttpRequestManager::getInstance()
{
if (!instance) {
QMutexLocker locker(&m_mutex);
if (!instance) {
instance = new HttpRequestManager();
}
}
return instance;
}
void HttpRequestManager::get(const QString &url)
{
QUrl qurl(url);
QNetworkRequest request(qurl);
// 设置ssl
SslConfiguationTool::setSslConfiguation(request);
m_reply = m_networkManager->get(request);
connect(m_reply, SIGNAL(finished()), this, SLOT(onHttpGetFinished()));
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
SLOT(onHttpGetError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(sslErrors(QList<QSslError>)), this,
SLOT(onHttpGetSslError(QList<QSslError>)));
}
void HttpRequestManager::onTimeOut()
{
qWarning() << "请求超时.";
if (m_reply)
m_reply->abort();
}
void HttpRequestManager::onHttpGetFinished()
{
m_timer->stop();
if (m_reply->error() == QNetworkReply::NoError) {
QVariant status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (status.isValid())
qInfo() << "response code:" << status;
QVariant reason = m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute);
if (reason.isValid())
qInfo() << "Http Reason:" << reason;
QString response = m_reply->readAll();
qInfo() << "response :" << response;
} else {
qWarning() << "获取返回数据失败.";
}
m_reply->deleteLater();
m_reply = nullptr;
}
void HttpRequestManager::onHttpGetError(QNetworkReply::NetworkError code)
{
QMetaEnum metaMnum = QMetaEnum::fromType<QNetworkReply::NetworkError>();
QString error = metaMnum.valueToKey(code);
qWarning() << "地址调用失败 :" << error;
}
void HttpRequestManager::onHttpGetSslError(const QList<QSslError> &errors)
{
qWarning() << "SSL 错误.";
foreach (QSslError error, errors) {
qWarning() << error.errorString();
}
}
HttpRequestManager::HttpRequestManager(QObject *parent) : QObject(parent)
{
if (!m_networkManager)
m_networkManager = new QNetworkAccessManager();
// 定义一个计时器用来做超时处理
m_timer = new QTimer();
m_timer->setInterval(30000);
m_timer->setSingleShot(true);
connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
}