Qt implements HTTP Get/Post requests

With the help of Qt's NetWork module, HTTP Get/Post requests can be easily implemented without referencing third-party libraries like libcurl again.
Of course, the functions provided by Qt's NetWork module are far more than just HTTP.

head File

#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>

In addition, using the Qt network module also needs to refer to the Qt5Network.lib library.

Get

First construct a QNetworkAccessManager object, the QNetworkAccessManager object provides the function of sending QNetworkRequest network request and receiving QNetworkReply network reply.

QNetworkAccessManager also provides cache and cookie management, proxy settings and other functions.

QNetworkRequest provides the encapsulation of this network request. In this example, only the simplest requset is constructed without any parameter setting. QNetworkRequest provides many methods to configure the request, for example, we can use QNetworkRequest::setHeader to set the request header and so on.

void QtGuiApplication::onBtnGetClicked() {
    QNetworkRequest request;
    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    Q_ASSERT(connRet);

    request.setUrl(QUrl("https://www.baidu.com"));
    QNetworkReply* reply = naManager->get(request);
}

The request is asynchronous. When the request is completed, void requestFinished(QNetworkReply* reply); slot function will be called:

void QtGuiApplication::requestFinished(QNetworkReply* reply) {
    // 获取http状态码
    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(statusCode.isValid())
        qDebug() << "status code=" << statusCode.toInt();

    QVariant reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    if(reason.isValid())
        qDebug() << "reason=" << reason.toString();

    QNetworkReply::NetworkError err = reply->error();
    if(err != QNetworkReply::NoError) {
        qDebug() << "Failed: " << reply->errorString();
    }
    else {
        // 获取返回内容
        qDebug() << reply->readAll();
    }
}

Post

void QtGuiApplication::onBtnPushClicked() {
    QNetworkRequest request;
    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    Q_ASSERT(connRet);

    request.setUrl(QUrl("https://www.baidu.com"));
    
    QString testData = "test";
    QNetworkReply* reply = naManager->post(request, testData.toUtf8());
}

Similarly, the request is also asynchronous. When the request is completed, the void requestFinished(QNetworkReply* reply); slot function (same as Get):

void QtGuiApplication::requestFinished(QNetworkReply* reply) {
    // 获取http状态码
    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(statusCode.isValid())
        qDebug() << "status code=" << statusCode.toInt();

    QVariant reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    if(reason.isValid())
        qDebug() << "reason=" << reason.toString();

    QNetworkReply::NetworkError err = reply->error();
    if(err != QNetworkReply::NoError) {
        qDebug() << "Failed: " << reply->errorString();
    }
    else {
        // 获取返回内容
        qDebug() << reply->readAll();
    }
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/130511454