370 lines
10 KiB
C++
370 lines
10 KiB
C++
#include "NetMgr.h"
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkRequest>
|
|
#include <QUrlQuery>
|
|
#include <QHttpMultiPart>
|
|
#include <QHttpPart>
|
|
#include <QDebug>
|
|
#include <QSslSocket>
|
|
#include <QSslConfiguration>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QSysInfo>
|
|
#include <QFile>
|
|
#include <QPointer>
|
|
#include <QDir>
|
|
#include <QMessageAuthenticationCode>
|
|
#include "log.h"
|
|
#include "global.h"
|
|
|
|
enum{
|
|
HTTP_CMD_ID = QNetworkRequest::User+100,
|
|
};
|
|
|
|
NetMgr *g_NetMgr = NULL;
|
|
NetMgr &NetMgr::instance()
|
|
{
|
|
static NetMgr ins;
|
|
return ins;
|
|
}
|
|
|
|
NetMgr::NetMgr(QObject *parent)
|
|
:QObject(parent)
|
|
,m_netMgr(new QNetworkAccessManager(this))
|
|
{
|
|
qDebug()<<m_netMgr->supportedSchemes();
|
|
connect(m_netMgr,SIGNAL(finished(QNetworkReply *)),SLOT(httpFinished(QNetworkReply *)));
|
|
}
|
|
|
|
NetMgr::~NetMgr()
|
|
{
|
|
}
|
|
|
|
void NetMgr::RequestFileList(const QString &sAddr)
|
|
{
|
|
QString sUrl = QString("http://%1%2").arg(sAddr).arg("/cgi-bin/General.cgi");
|
|
qDebug()<<"url[RequestLicList]:"<<sUrl;
|
|
QJsonObject jsObj;
|
|
jsObj.insert("type", "GET");
|
|
jsObj.insert("page", 1);
|
|
jsObj.insert("number", 20);
|
|
QJsonObject allObj;
|
|
allObj.insert("cmd", "39");
|
|
allObj.insert("cmdBody",jsObj);
|
|
|
|
QNetworkRequest req;
|
|
QString sFlag = sAddr + "+RequestFileList";
|
|
setReqUserAttr(req, sFlag);
|
|
req.setUrl(sUrl);
|
|
PostJson(req,allObj);
|
|
}
|
|
|
|
void NetMgr::RequestFileInfo(const QString &sAddr)
|
|
{
|
|
QString sUrl = QString("http://%1%2").arg(sAddr).arg("/cgi-bin/General.cgi"); //路径对不对
|
|
qDebug()<<"url[RequestFileInfo]:"<<sUrl;
|
|
QJsonObject jsObj;
|
|
jsObj.insert("type", "GET");
|
|
QJsonObject allObj;
|
|
allObj.insert("cmd", "20");
|
|
allObj.insert("cmdBody",jsObj);
|
|
|
|
QNetworkRequest req;
|
|
QString sFlag = sAddr + "+RequestFileInfo";
|
|
setReqUserAttr(req, sFlag);
|
|
req.setUrl(sUrl);
|
|
|
|
PostJson(req,allObj);
|
|
}
|
|
|
|
void NetMgr::RequestDownload(const QString &sAddr, const QString& sFileName,const QString &sFileSavePath)
|
|
{
|
|
QString sUrl = QString("http://%1%2%3").arg(sAddr).arg("/CIDW/config/").arg(sFileName);
|
|
|
|
//qDebug()<<"url[RequestDownload]:"<<sUrl;
|
|
QNetworkRequest req;
|
|
setReqUserAttr(req, sAddr);
|
|
req.setUrl(sUrl);
|
|
DownLoad(req,sFileSavePath);
|
|
}
|
|
void NetMgr::RequestChannelInfo(const QString& sAddr)
|
|
{
|
|
QString sUrl = QString("http://%1%2").arg(sAddr).arg("/cgi-bin/General.cgi");
|
|
//qDebug()<<"url[RequestChannelInfo]:"<<sUrl;
|
|
QJsonObject jsObj;
|
|
jsObj.insert("type", "GET");
|
|
QJsonObject allObj;
|
|
allObj.insert("cmd", "04");
|
|
allObj.insert("cmdBody",jsObj);
|
|
|
|
QNetworkRequest req;
|
|
QString sFlag = sAddr + "+RequestChannelInfo";
|
|
setReqUserAttr(req, sFlag);
|
|
req.setUrl(sUrl);
|
|
PostJson(req,allObj);
|
|
}
|
|
void NetMgr::PostMultiPart(QNetworkRequest &req, QMap<QString, QString> &postData)
|
|
{
|
|
AddMultiPartSignature(req, postData);
|
|
qDebug()<<"suport ssl"<<QSslSocket::supportsSsl();
|
|
|
|
QSslConfiguration config;
|
|
config.setProtocol(QSsl::TlsV1SslV3);
|
|
config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
req.setSslConfiguration(config);
|
|
|
|
//QString sBody;
|
|
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
|
for(auto itr = postData.begin(); itr != postData.end(); ++itr)
|
|
{
|
|
QHttpPart textPart;
|
|
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg(itr.key())));
|
|
textPart.setBody(itr.value().toUtf8());
|
|
multiPart->append(textPart);
|
|
}
|
|
|
|
|
|
QNetworkReply* pReply = m_netMgr->post(req,multiPart);
|
|
multiPart->setParent(pReply);
|
|
|
|
connect(pReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[=](QNetworkReply::NetworkError err)
|
|
{
|
|
qDebug()<<"net error:";
|
|
qDebug()<<pReply->errorString();
|
|
//pReply->deleteLater();
|
|
});
|
|
|
|
}
|
|
|
|
void NetMgr::PostJson(QNetworkRequest &req, QJsonObject &postData)
|
|
{
|
|
AddJsonSignature(req, postData);
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
|
|
QSslConfiguration config;
|
|
config.setProtocol(QSsl::TlsV1SslV3);
|
|
config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
req.setSslConfiguration(config);
|
|
|
|
//QString sBody;
|
|
QByteArray bzData = QJsonDocument(postData).toJson(QJsonDocument::Compact);
|
|
QNetworkReply* pReply = m_netMgr->post(req,bzData);
|
|
customLogMessageHandler(QtDebugMsg,QString(QJsonDocument(postData).toJson()));
|
|
connect(pReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[=](QNetworkReply::NetworkError err)
|
|
{
|
|
qDebug()<<"error:";
|
|
qDebug()<<pReply->errorString();
|
|
//pReply->deleteLater();
|
|
});
|
|
}
|
|
|
|
void NetMgr::PostData(QNetworkRequest &req, QByteArray &postData)
|
|
{
|
|
AddDataSignature(req, postData);
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
|
|
QSslConfiguration config;
|
|
config.setProtocol(QSsl::TlsV1SslV3);
|
|
config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
req.setSslConfiguration(config);
|
|
|
|
QNetworkReply* pReply = m_netMgr->post(req,postData);
|
|
|
|
connect(pReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[=](QNetworkReply::NetworkError err)
|
|
{
|
|
qDebug() << "error:";
|
|
qDebug() << pReply->errorString();
|
|
//pReply->deleteLater();
|
|
});
|
|
}
|
|
|
|
void NetMgr::DownLoad(QNetworkRequest &req, const QString &sFileSavePath)
|
|
{
|
|
QSslConfiguration config;
|
|
config.setProtocol(QSsl::TlsV1SslV3);
|
|
config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
req.setSslConfiguration(config);
|
|
|
|
QString sDir = QFileInfo(sFileSavePath).dir().path();
|
|
QDir dir(sDir);
|
|
if(!dir.exists())
|
|
{
|
|
if(!dir.mkpath(sDir))
|
|
{
|
|
qDebug()<<"mkpath failed"<<sDir;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
QPointer<QFile> pFile(new QFile(sFileSavePath));
|
|
if(!pFile->open(QIODevice::WriteOnly|QIODevice::Truncate))
|
|
{
|
|
|
|
qDebug()<<pFile->errorString()<<sFileSavePath;
|
|
delete pFile;
|
|
return;
|
|
}
|
|
QNetworkReply* pReply = m_netMgr->get(req);
|
|
m_mpDownload[pReply] = pFile;
|
|
pFile->setParent(pReply);
|
|
connect(pReply,&QNetworkReply::readyRead,[=]
|
|
{
|
|
if(!pFile.isNull())
|
|
{
|
|
pFile->write(pReply->readAll());
|
|
}
|
|
|
|
});
|
|
|
|
connect(pReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[=](QNetworkReply::NetworkError err)
|
|
{
|
|
//qDebug()<<"error:";
|
|
//qDebug()<<pReply->errorString();
|
|
if(!pFile.isNull())
|
|
{
|
|
pFile->close();
|
|
QFile::remove(pFile->fileName());
|
|
}
|
|
pReply->deleteLater();
|
|
});
|
|
}
|
|
|
|
void NetMgr::GetData(QNetworkRequest &req)
|
|
{
|
|
//req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
|
|
QSslConfiguration config;
|
|
config.setProtocol(QSsl::TlsV1SslV3);
|
|
config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
req.setSslConfiguration(config);
|
|
|
|
//QString sBody;
|
|
|
|
QNetworkReply* pReply = m_netMgr->get(req);
|
|
|
|
connect(pReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[=](QNetworkReply::NetworkError err)
|
|
{
|
|
//qDebug()<<"error:";
|
|
//qDebug()<<pReply->errorString();
|
|
//pReply->deleteLater();
|
|
});
|
|
}
|
|
|
|
|
|
void NetMgr::AddMultiPartSignature(QNetworkRequest &req, QMap<QString, QString> &postData)
|
|
{
|
|
QString sSrcKey;
|
|
QStringList listParams;
|
|
foreach(const QString& sKey, postData.keys())
|
|
{
|
|
listParams << QString("%1=%2").arg(sKey, postData.value(sKey));
|
|
}
|
|
sSrcKey = listParams.join("&");
|
|
AddSignature(req, sSrcKey);
|
|
|
|
}
|
|
|
|
void NetMgr::AddJsonSignature(QNetworkRequest &req, QJsonObject &postData)
|
|
{
|
|
QString sSrcKey;
|
|
//if(!postData.isEmpty())
|
|
{
|
|
sSrcKey = QJsonDocument(postData).toJson(QJsonDocument::Compact);
|
|
}
|
|
|
|
AddSignature(req, sSrcKey);
|
|
}
|
|
|
|
void NetMgr::AddDataSignature(QNetworkRequest &req, QByteArray &postData)
|
|
{
|
|
Q_UNUSED(postData)
|
|
QString sSrcKey;
|
|
AddSignature(req, sSrcKey);
|
|
}
|
|
|
|
void NetMgr::AddSignature(QNetworkRequest &req, QString& sSrcKey)
|
|
{
|
|
qDebug() << sSrcKey;
|
|
QString sToken = "";
|
|
if (req.hasRawHeader("commtoken"))
|
|
{
|
|
sToken = req.rawHeader("commtoken");
|
|
}
|
|
else if (req.hasRawHeader("accesstoken"))
|
|
{
|
|
sToken = req.rawHeader("accesstoken");
|
|
}
|
|
QString sTimeStamp = QString::number(QDateTime::currentMSecsSinceEpoch());
|
|
if (sSrcKey.isEmpty())
|
|
{
|
|
sSrcKey = QString("timestamp=%1").arg(sTimeStamp);
|
|
}
|
|
else {
|
|
sSrcKey = sSrcKey + QString("×tamp=%1").arg(sTimeStamp);
|
|
}
|
|
|
|
sSrcKey = QUrl::toPercentEncoding(sSrcKey);
|
|
QByteArray key = sToken.toUtf8();
|
|
QByteArray message = sSrcKey.toUtf8();
|
|
QByteArray bzSign = QMessageAuthenticationCode::hash(message, key, QCryptographicHash::Sha256).toHex();
|
|
bzSign = bzSign.toBase64();
|
|
req.setRawHeader("timestamp", sTimeStamp.toUtf8());
|
|
req.setRawHeader("signature", bzSign);
|
|
}
|
|
|
|
void NetMgr::httpFinished(QNetworkReply *reply)
|
|
{
|
|
if (reply->error() == QNetworkReply::NoError)
|
|
{
|
|
if (m_mpDownload.contains(reply))
|
|
{
|
|
QString sAddr = reply->request().attribute((QNetworkRequest::Attribute)HTTP_CMD_ID).toString();
|
|
reply->waitForReadyRead(3000);
|
|
QFile* pFile = m_mpDownload.value(reply);
|
|
//emit sigNetMgr(sAddr, pFile->fileName());
|
|
m_mpDownload.remove(reply);
|
|
}
|
|
else
|
|
{
|
|
QString sAddr = reply->request().attribute((QNetworkRequest::Attribute)HTTP_CMD_ID).toString();
|
|
//qDebug()<<"nCmdId:"<<nCmdId;
|
|
QByteArray bzData = reply->readAll();
|
|
QString sPath = reply->request().url().path();
|
|
parseData(bzData, sPath, sAddr);
|
|
}
|
|
|
|
}
|
|
else
|
|
{//错误
|
|
QMessageBox::warning(NULL, "提示", "通信失败!");
|
|
}
|
|
|
|
reply->deleteLater();
|
|
}
|
|
|
|
void NetMgr::parseData(const QByteArray &szData, const QString &sUrlPath, QString sAddr)
|
|
{
|
|
//qDebug() << sUrlPath << szData;
|
|
QJsonDocument doc = QJsonDocument::fromJson(szData);
|
|
QJsonObject obj = doc.object();
|
|
QVariant var;
|
|
var.setValue(obj);
|
|
customLogMessageHandler(QtDebugMsg,QString(QJsonDocument(obj).toJson()));
|
|
emit sigNetMgr(sAddr, var);
|
|
}
|
|
|
|
void NetMgr::setReqUserAttr(QNetworkRequest &req, QVariant attr)
|
|
{
|
|
req.setAttribute((QNetworkRequest::Attribute)HTTP_CMD_ID,attr);
|
|
}
|
|
|
|
void NetMgr::DetectNet()
|
|
{
|
|
QJsonObject allObj;
|
|
allObj.insert("cmd", "110");
|
|
QNetworkRequest req;
|
|
QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP);
|
|
req.setUrl(sUrl);
|
|
PostJson(req,allObj);
|
|
}
|
|
|