第一步:
pro引入
QT += network
第二步:
.h
#include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkAccessManager> Q_OBJECT // 使用槽,信号必须要加入此宏 signals: void m_request_fail(int code,QString msg); public: QByteArray sendPostRequest(QNetworkRequest request,QByteArray sendData); QByteArray sendGetRequest(QNetworkRequest request); protected: // 成员变量 QNetworkAccessManager *m_pHttpMgr; QTimer* m_requestTimeoutTimer;
.cpp
// 构造方法初始化 m_pHttpMgr = new QNetworkAccessManager(this); qDebug() << "m_pHttpMgr:" << m_pHttpMgr->supportedSchemes(); m_requestTimeoutTimer = new QTimer(m_pHttpMgr); m_requestTimeoutTimer->setSingleShot(true);// 单次触发 m_requestTimeoutTimer->setInterval(1000); /** 用法 //设置url QString url = "http://10.0.0.20:8005/order/charge"; //设置头信息 QNetworkRequest requestInfo; requestInfo.setUrl(QUrl(url)); requestInfo.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json")); requestInfo.setRawHeader("Content-Type","application/json");//服务器要求的数据头部 //发送数据 QByteArray qByteHttpData = sendJson(); MyThread::sendPostRequest->sendPostRequest(requestInfo, qByteHttpData); * @brief MyThread::sendPostRequest * @param request * @param sendData * @return */ QByteArray MyThread::sendPostRequest(QNetworkRequest request,QByteArray sendData){ QNetworkReply *reply = m_pHttpMgr->post(request, sendData); //添加事件循环机制,返回后再运行后面的 QEventLoop eventLoop; connect(m_requestTimeoutTimer,&QTimer::timeout,&eventLoop,&QEventLoop::quit); connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); m_requestTimeoutTimer->start(); eventLoop.exec(); //block until finish if(m_requestTimeoutTimer->isActive()){ m_requestTimeoutTimer->stop(); //错误处理 if (reply->error() == QNetworkReply::NoError) { qDebug() << "request protobufHttp NoError"; } else { qDebug()<<"request protobufHttp handle errors here"; QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); //statusCodeV是HTTP服务器的相应码,reply->error()是Qt定义的错误码,可以参考QT的文档 qDebug() << "request protobufHttp found error ....code: %d %d\n" << statusCodeV.toInt() << (int)reply->error(); qDebug() << "request error fail:" << reply->errorString(); emit m_request_fail(statusCodeV.toInt(),reply->errorString()); } }else{ disconnect(m_requestTimeoutTimer,&QTimer::timeout,&eventLoop,&QEventLoop::quit); emit m_request_fail(-1,"timeout"); } //请求收到的结果 QByteArray response = reply->readAll(); reply->abort(); reply->deleteLater(); return response; } /** 用法 //设置url QString url = "http://10.0.0.20:8005/vending_machine/items/ac83f39ea268";//fae4b325e52c QNetworkRequest requestInfo; requestInfo.setUrl(QUrl(url)); myThread->sendGetRequest(requestInfo); * @brief MyThread::sendGetRequest * @param request * @return */ QByteArray MyThread::sendGetRequest(QNetworkRequest request){ //添加事件循环机制,返回后再运行后面的 QEventLoop eventLoop; QNetworkReply *reply = m_pHttpMgr->get(request); connect(m_requestTimeoutTimer,&QTimer::timeout,&eventLoop,&QEventLoop::quit); connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit())); m_requestTimeoutTimer->start(); eventLoop.exec(); //block until finish if(m_requestTimeoutTimer->isActive()){ //错误处理 if (reply->error() == QNetworkReply::NoError) { qDebug() << "request protobufHttp NoError"; } else { qDebug()<<"request protobufHttp handle errors here"; QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); //statusCodeV是HTTP服务器的相应码,reply->error()是Qt定义的错误码,可以参考QT的文档 qDebug() << "request protobufHttp found error ....code: %d %d\n" << statusCodeV.toInt() << (int)reply->error(); emit m_request_fail(statusCodeV.toInt(),reply->errorString()); } }else{ qDebug() << "timeout"; disconnect(m_requestTimeoutTimer,&QTimer::timeout,&eventLoop,&QEventLoop::quit); emit m_request_fail(-1,"timeout"); } //请求返回的结果 QByteArray response = reply->readAll(); reply->abort(); reply->deleteLater(); return response; }