QT 發送HTTP請求

今天領導讓研究一下QT程序請求我寫的網站後臺數據,本人QT小白只寫過三個小工具,因此到網上找了一些資料並寫了一個小demo: 工具

HTTP請求在QT中相應的類爲NetworkRequest,HTTP響應的類爲NetworkReply。發送請求須要建立一個管理器實例QNetworkAccessManager。能夠調用它的get、post等方法發送get、post等HTTP請求。 post

class HttpTest : public QObject{
Q_OBJECT
public:
    void testGet(); //發送http請求
public slots:
    void replyFinish(QNetworkReply*); //用於處理響應返回的數據
}; 網站


void HttpTest::testGet(){
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QNetworkRequest request;

    request.setUrl(QUrl("http://192.168.1.1:8080/pvstation/status"));
    QNetworkReply* reply = manager->get(request);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinish(QNetworkReply*)));
}


void HttpTest::replyFinish(QNetworkReply* reply)
{
    qDebug()<<reply->readAll();
}
this

相關文章
相關標籤/搜索