1. 程式人生 > 其它 >qt 使用http請求,處理超時錯誤

qt 使用http請求,處理超時錯誤

第一步:

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; }