qt发送https请求,屏蔽ssl证书

//.h

#ifndef HTTPCOMMUNICATION_H
#define HTTPCOMMUNICATION_H

#include <QObject>
#include <QNetworkAccessManager>

/****************************************************************/

class HttpCommunication : public QObject
{
Q_OBJECT

public:
/****************************************************************/
HttpCommunication(QObject *parent = nullptr);
~HttpCommunication();
/****************************************************************/
public:
static HttpCommunication& GetInstance();
void SetHttpInfo(QString strPlatformIp,QString strToken,QString strUser); //设置平台信息 strPlatformIp:http://ip:port
QString GetRequestData(QString strUrl);
QString PostRequestFindCases(int nPagNo,int nPagesize); 
QString GetRequestGetCamera(int nPagNo, int nPagesize,QString strParenrIndex); 
QString GetConfigUiUrl(); //获取前端配置界面的url,放到客户端展示
private:
QNetworkAccessManager* m_qNetWork; //定义
QString m_strPlatformIp; //平台Ip和端口
QString m_strToken; //token值
QString m_strCurrentUser; //当前客户端登陆用户
};

#endif // HTTPCOMMUNICATION_H

//cpp

#include <QJsonObject>
#include <QJsonDocument>
#include <QEventLoop>
#include <QNetworkReply>

#include "Httpcommunication.h"
#include "../LogDefinition.h"

/****************************************************************/
HttpCommunication::HttpCommunication(QObject *parent)
: QObject(parent)
{
m_qNetWork = nullptr;
m_qNetWork = new QNetworkAccessManager(this); //分配控件
}

HttpCommunication::~HttpCommunication()
{

}
/****************************************************************/
/****************************************************************/
/**********************public************************************/
/****************************************************************/
HttpCommunication& HttpCommunication::GetInstance()
{
static HttpCommunication instance;
return instance;
}
void HttpCommunication::SetHttpInfo(QString strPlatformIp, QString strToken, QString strUser)
{
m_strPlatformIp = strPlatformIp;
m_strToken = strToken;
m_strCurrentUser = strUser;
}
QString HttpCommunication::GetRequestData(QString strUrl)
{
return "";
}

QString HttpCommunication::PostRequestFindCases(int nPagNo, int nPagesize)
{
if (m_qNetWork == nullptr)
m_qNetWork = new QNetworkAccessManager(this);

//1、组装url
QString strUrl = QString("%1/sphfkssjpz/api/v1/findCases").arg(m_strPlatformIp);
DRV_LOG_INFO("PostRequestFindCases strUrl = %s", strUrl.toStdString().c_str());
//2、组装JSON
QByteArray postData;
QJsonObject jsonObject;
jsonObject.insert("userId", m_strCurrentUser);
jsonObject.insert("caseName", "");
jsonObject.insert("endTime", "");
jsonObject.insert("pageNo", nPagNo);
jsonObject.insert("pageSize", nPagesize);
jsonObject.insert("personName", "");
jsonObject.insert("regionName", "");
jsonObject.insert("startTime", "");
QString sData = QJsonDocument(jsonObject).toJson();
postData.append(sData);

DRV_LOG_INFO("PostRequestFindCases sData = %s", sData.toStdString().c_str());

QNetworkRequest objRequest;
QSslConfiguration sslConfig;
sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfig.setProtocol(QSsl::TlsV1SslV3);
objRequest.setSslConfiguration(sslConfig);

objRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
objRequest.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());
//objRequest.setRawHeader("token", m_strToken.toUtf8());
objRequest.setUrl(QUrl(strUrl));

//发送请求及获取数据
QNetworkReply *reply = m_qNetWork->post(objRequest, postData);
QEventLoop eventloop;
QObject::connect(reply, &QNetworkReply::finished, &eventloop, &QEventLoop::quit);
eventloop.exec(QEventLoop::ExcludeUserInputEvents);
QByteArray strRecvArray = reply->readAll();
reply->deleteLater();
QString strRecv(strRecvArray);

return strRecv;
}
QString HttpCommunication::GetRequestGetCamera(int nPagNo, int nPagesize,QString strParenrIndex)
{
if (m_qNetWork == nullptr)
m_qNetWork = new QNetworkAccessManager(this);

//1、组装url
QString strUrl = QString("%1/sphfkssjpz/api/v1/getResourcesByIndexCode?userId=%2&parentIndexCodes=%3&pageNo=%4&pageSize=%5").
arg(m_strPlatformIp).arg(m_strCurrentUser).arg(strParenrIndex).arg(QString::number(nPagNo)).arg(QString::number(nPagesize));
DRV_LOG_INFO("GetRequestGetCamera strUrl = %s", strUrl.toStdString().c_str());
//2、发送get请求
QNetworkRequest objRequest;

QSslConfiguration sslConfig;
sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfig.setProtocol(QSsl::TlsV1SslV3);
objRequest.setSslConfiguration(sslConfig);

objRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
objRequest.setRawHeader("token", m_strToken.toUtf8());
objRequest.setUrl(QUrl(strUrl));

//发送请求及获取数据
QNetworkReply *reply = m_qNetWork->get(objRequest);
QEventLoop eventloop;
QObject::connect(reply, &QNetworkReply::finished, &eventloop, &QEventLoop::quit);
eventloop.exec(QEventLoop::ExcludeUserInputEvents);
QByteArray strRecvArray = reply->readAll();
reply->deleteLater();
QString strRecv(strRecvArray);

return strRecv;
}
/*************
{
"type":0,
"code":"0",
"msg":"操作成功",
"data":{
"url1":"value1",
"url2":"value2"
}
}
*************/
QString HttpCommunication::GetConfigUiUrl()
{
if (m_qNetWork == nullptr)
m_qNetWork = new QNetworkAccessManager(this);
//1、组装url
QString strUrl = QString("%1/sphfkssjpz/api/v1/getPageVisitUrl").arg(m_strPlatformIp);//https://10.196.1.62:1443/sphfkssjpz/api/v1/getPageVisitUrl
DRV_LOG_INFO("GetConfigUiUrl strUrl = %s", strUrl.toStdString().c_str());
//2、发送get请求
QNetworkRequest objRequest;

QSslConfiguration sslConfig;
sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfig.setProtocol(QSsl::TlsV1SslV3);
objRequest.setSslConfiguration(sslConfig);

objRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
objRequest.setRawHeader("token", m_strToken.toUtf8());
objRequest.setUrl(QUrl(strUrl));

//发送请求及获取数据
QNetworkReply *reply = m_qNetWork->get(objRequest);
QEventLoop eventloop;
QObject::connect(reply, &QNetworkReply::finished, &eventloop, &QEventLoop::quit);
eventloop.exec(QEventLoop::ExcludeUserInputEvents);
QByteArray strRecvArray = reply->readAll();
reply->deleteLater();
QString strRecv(strRecvArray);

//3、解析返回过来的URL
QJsonDocument document = QJsonDocument::fromJson(strRecv.toUtf8().data());
QJsonObject jsonObjectResult = document.object();
QString strCode = jsonObjectResult["code"].toString();
QString strMsg = jsonObjectResult["msg"].toString();

QJsonObject jsonObjectData = jsonObjectResult["data"].toObject();

QStringList strUrlList;
for (int i = 0; i < jsonObjectData.size(); i++)
{
QString strKeyUrl = QString("url%1").arg(QString::number(i + 1));
QString strConfigUrl = jsonObjectData[strKeyUrl].toString();
strUrlList.append(strConfigUrl);
}
DRV_LOG_INFO("GetConfigUiUrl strUrlList[0] = %s", strUrlList[0].toStdString().c_str());

return strUrlList[0];
}
/****************************************************************/
/**********************protected*********************************/
/****************************************************************/

/****************************************************************/
/**********************private***********************************/
/****************************************************************/

posted @ 2022-03-11 14:28  冥天笑  阅读(809)  评论(0编辑  收藏  举报