#pragma execution_character_set("utf-8")
#include "netserver.h"
#include <QDebug>
using namespace NetWorkUtls;
QString NetServer::m_strCookie = "";
bool NetServer::m_bExistSession = false;
NetServer::NetServer(bool bSaveSessionId, QObject *parent)
: QObject(parent)
, m_bIsTimeOut(false)
, m_bSaveSessionId(bSaveSessionId)
{
m_pNetwork = new QNetworkAccessManager(this);
m_pTimer = new QTimer(this);
m_pLoop = new QEventLoop(this);
connect(m_pTimer, &QTimer::timeout, this, &NetServer::slotTimeOut);
}
//同步
void NetServer::syncGet(const QString& url, int timeout)
{
disconnect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveReplay);
disconnect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveAsync);
connect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveReplay);
QNetworkRequest request;
request.setUrl(QUrl(url));
if(!NetServer::m_strCookie.isEmpty())
request.setRawHeader("Cookie", NetServer::m_strCookie.toUtf8());
m_bIsTimeOut = false;
m_pTimer->setInterval(timeout);
m_pTimer->setSingleShot(true);
m_pTimer->start();
logStart(url);
m_pNetwork->get(request)->ignoreSslErrors();
m_pLoop->exec();
}
//异步
QNetworkReply* NetServer::get(const QString& url)
{
disconnect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveReplay);
disconnect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveAsync);
connect(m_pNetwork, &QNetworkAccessManager::finished, this, &NetServer::slotReceiveAsync);
QNetworkRequest request;
request.setUrl(QUrl(url));
if(!NetServer::m_strCookie.isEmpty())
request.setRawHeader("Cookie", NetServer::m_strCookie.toUtf8());
m_bIsTimeOut = false;
logStart(url);
QNetworkReply* reply = m_pNetwork->get(request);
reply->ignoreSslErrors();
return reply;
}
void NetServer::slotTimeOut()
{
logTimeOut();
m_bIsTimeOut = true;
m_pLoop->quit();
}
void NetServer::slotReceiveReplay(QNetworkReply* reply)
{
if(m_bSaveSessionId)
{
QString re = reply->rawHeader("Set-Cookie");
QStringList strCookies = re.split(";");
for(int i=0; i<strCookies.length(); i++)
{
QString strCookie = strCookies.at(i);
if(strCookie.startsWith("seco="))
{
NetServer::m_strCookie = strCookie;
break;
}
}
}
if(m_pTimer->isActive())
{
m_pTimer->stop();
m_pReply = reply->readAll();
if(reply->error() == QNetworkReply::NoError)
{
logEnd(m_pReply);
}
else
{
logError(reply->errorString());
}
m_pLoop->quit();
}
reply->deleteLater();
}
void NetServer::slotReceiveAsync(QNetworkReply *reply)
{
QByteArray data = reply->readAll();
if(reply->error() == QNetworkReply::NoError)
{
logEnd(data);
}
else
{
logError(reply->errorString());
}
emit finished(data);
reply->deleteLater();
}
void NetServer::logStart(const QString &url)
{
m_url = url;
m_dateTime = QDateTime::currentDateTime();
qDebug() << QStringLiteral("请求开始: ") << m_url;
}
void NetServer::logEnd(const QByteArray& data)
{
qDebug() << QStringLiteral("请求结束: ")
<< m_url
<< QStringLiteral(" 共耗时: ")
<< m_dateTime.msecsTo(QDateTime::currentDateTime())
<< "ms"
<< QStringLiteral(" 数据内容: ")
<< data;
}
void NetServer::logTimeOut()
{
qDebug() << QStringLiteral("请求超时: ") << m_url;
}
void NetServer::logError(const QString &error)
{
qDebug() << QStringLiteral("请求出错: ") << m_url << QStringLiteral("错误描述: ") << error;
}
PostNetServer::PostNetServer(QObject *parent) :
QObject(parent)
{
m_manager = new QNetworkAccessManager(this);
connect(m_manager, &QNetworkAccessManager::finished, this, &PostNetServer::replyFinished);
m_pTimer = new QTimer(this);
m_pTimer->setSingleShot(true);
connect(m_pTimer, &QTimer::timeout,
this, &PostNetServer::slotTimeOut);
}
void PostNetServer::post(const QString& strUrl, const QByteArray& strData)
{
QSslConfiguration config;
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::SslV3);
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
request.setSslConfiguration(config);
request.setUrl(QUrl(strUrl));
logStart(strUrl);
m_manager->post(request, strData);
}
void PostNetServer::post(const QString &strUrl, const QByteArray &strData, int timeout)
{
m_pTimer->start(timeout);
post(strUrl, strData);
}
void PostNetServer::replyFinished(QNetworkReply *reply)
{
m_pTimer->stop();
if(reply && reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
logEnd(data);
int len = data.size();
emit this->response(len);
emit this->receiveData(data);
}
else
{
logError(reply->errorString());
emit this->error((int)reply->error());
}
reply->deleteLater();
}
void PostNetServer::slotTimeOut()
{
logTimeOut();
emit error(QNetworkReply::TimeoutError);
}
void PostNetServer::logStart(const QString &url)
{
m_url = url;
m_dateTime = QDateTime::currentDateTime();
qDebug() << QStringLiteral("请求开始: ") << m_url;
}
void PostNetServer::logEnd(const QByteArray& data)
{
qDebug() << QStringLiteral("请求结束: ")
<< m_url
<< QStringLiteral(" 共耗时: ")
<< m_dateTime.msecsTo(QDateTime::currentDateTime())
<< "ms"
<< QStringLiteral(" 数据内容: ")
<< data.data();
}
void PostNetServer::logTimeOut()
{
qDebug() << QStringLiteral("请求超时: ") << m_url;
}
void PostNetServer::logError(const QString& error)
{
qDebug() << QStringLiteral("请求出错: ") << m_url << QStringLiteral("错误描述: ") << error;
}
- 1
- 2
前往页