- 头文件
#ifndef NETWORKHELPER_H
#define NETWORKHELPER_H
#include <QObject>
#include <QNetworkReply>
#include <QTimer>
#define LimitTimeout 20000 // 请求超时时间(毫秒)
typedef void (*ReceiveFun)(int code, const QByteArray& bytes, void* pCusData);
class NetworkHelper : public QObject
{
Q_OBJECT
public:
static void get(const QString& url, ReceiveFun receiver, void* data, int timeout = LimitTimeout);
static void post(const QString& url, const QString& content, ReceiveFun receiver, void* data, int timeout = LimitTimeout);
static void postJson(const QString& url, const QString& content, ReceiveFun receiver, int timeout = LimitTimeout);
private slots:
void OnReplyFinished(QNetworkReply* reply);
void OnTimer();
private:
NetworkHelper(QString url, QString content, ReceiveFun receiver, void* pCusData, int timeout);
~NetworkHelper();
void executeGet();
void executePost();
void executePostJson();
private:
QNetworkAccessManager* m_pNetworkAccessManager;
QTimer* m_pTimer;
QString m_url;
QString m_content;
ReceiveFun m_receiver;
void* m_pCusData;
int m_timeout;
signals:
public slots:
};
#endif // NETWORKHELPER_H
- 源文件
#include "NetworkHelper.h"
#include "const/EnumData.h"
#include <QDebug>
/**
* @brief 网络通讯辅助类构造函数
* @author Robin
* @date 2020-02-25
* @param url
* @param content
* @param receiver
* @param pCusData
* @param timeout
*/
NetworkHelper::NetworkHelper(QString url, QString content, ReceiveFun receiver, void* pCusData, int timeout)
{
m_url = url;
m_content = content;
m_receiver = receiver;
m_pCusData = pCusData;
m_timeout = timeout;
m_pNetworkAccessManager = new QNetworkAccessManager();
m_pTimer = new QTimer();
connect( m_pNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnReplyFinished(QNetworkReply*)) );
connect( m_pTimer, SIGNAL(timeout()), this, SLOT(OnTimer()) );
}
/**
* @brief 网络通讯辅助类析构函数
* @author Robin
* @date 2020-02-25
*/
NetworkHelper::~NetworkHelper()
{
m_pNetworkAccessManager->deleteLater();
m_pTimer->deleteLater();
}
/**
* @brief GET方法请求数据
* @author Robin
* @date 2020-02-25
* @param url
* @param receiver
* @param data
* @param timeout
*/
void NetworkHelper::get(const QString &url, ReceiveFun receiver, void *data, int timeout)
{
NetworkHelper* p = new NetworkHelper(url, "", receiver, data, timeout);
p->executeGet();
}
/**
* @brief POST方法请求数据
* @author Robin
* @date 2020-02-25
* @param url
* @param content
* @param receiver
* @param data
* @param timeout
*/
void NetworkHelper::post(const QString &url, const QString &content, ReceiveFun receiver, void *data, int timeout)
{
NetworkHelper* p = new NetworkHelper(url, content, receiver, data, timeout);
p->executePost();
}
/**
* @brief 使用POST方法发送JSON数据
* @author Robin
* @date 2020-02-25
* @param url
* @param content
* @param receiver
* @param timeout
*/
void NetworkHelper::postJson(const QString &url, const QString &content, ReceiveFun receiver, int timeout)
{
NetworkHelper* p = new NetworkHelper(url, content, receiver, NULL, timeout);
p->executePostJson();
}
/**
* @brief 接口响应槽函数
* @author Robin
* @date 2020-02-25
* @param reply
*/
void NetworkHelper::OnReplyFinished(QNetworkReply *reply)
{
m_pTimer->stop();
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
if (m_receiver)
{
m_receiver(NetworkReply::ok, bytes, m_pCusData);
}
}
else
{
if (m_receiver)
{
m_receiver(NetworkReply::error, QByteArray(), m_pCusData);
}
}
reply->deleteLater();
this->deleteLater();
}
/**
* @brief 定时器响应槽函数
* @author Robin
* @date 2020-02-25
*/
void NetworkHelper::OnTimer()
{
m_pTimer->stop();
if (m_receiver)
{
// 超时
m_receiver( NetworkReply::timeout, QByteArray(), m_pCusData);
}
this->deleteLater();
}
/**
* @brief 执行GET请求
* @author Robin
* @date 2020-02-25
*/
void NetworkHelper::executeGet()
{
QNetworkRequest network_request;
network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");//二进制流类型
network_request.setUrl( QUrl(m_url) );
m_pNetworkAccessManager->get(network_request);
m_pTimer->start(m_timeout);
}
/**
* @brief 执行POST请求
* @author Robin
* @date 2020-02-25
*/
void NetworkHelper::executePost()
{
QNetworkRequest network_request;
network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");//二进制流类型
network_request.setUrl( QUrl(m_url) );
QByteArray post_data;
post_data.append(m_content);
m_pNetworkAccessManager->post(network_request, post_data);
m_pTimer->start(m_timeout);
}
/**
* @brief 执行发送JSON数据
* @author Robin
* @date 2020-02-25
*/
void NetworkHelper::executePostJson()
{
QNetworkRequest network_request;
network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
network_request.setUrl(QUrl(m_url));
//m_content = m_content.toUtf8();
QByteArray post_data;
post_data.append(m_content);
m_pNetworkAccessManager->post(network_request, post_data);
m_pTimer->start(m_timeout);
}
完整的类已经放出,转载请注明出处。