banner
Alexeisie

AlexEisie

啊? Email: alexeisie@brs.red
github

Qt6/5 Network Programming - HTTP GET/POST Requests

The development platform is VS2022+Qt components, which may be slightly different from QtCreator.

1. Configure the Network library for the project#

image
There should be an include folder and a lib folder in the Qt installation directory, where you can find the QtNetwork folder and the Qt6Network.lib file (for Debug compilation, it is Qt6Networkd.lib, and for Qt5, it is Qt5Network.lib).
image
Add the following line to the Additional Include Directories in the project properties C/C++ -> General:

$(QTDIR)\include\QtNetwork

image
Add the following line to the Additional Dependencies in the project properties Linker -> Input:

$(QTDIR)\lib\Qt6Network.lib

For Release mode, configure the header and library files accordingly.

2. Header files#

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

QtNetwork is asynchronous, and each process needs a QNetworkAccessManager object to manage network objects. We will use QNetworkRequest for requests and QNetworkReply to get responses.

3. Function implementation#

//QtNetwork.cpp
connect(ui.GETButton, SIGNAL(clicked()), this, SLOT(onGETButtonClicked()));

Assuming that the click signal of GETButton is connected to the slot function onGETButtonClicked.

//QtNetwork.hpp private slots:
void onGETButtonClicked() {
    QNetworkRequest request;
    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

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

First, create QNetworkAccessManager and QNetworkRequest objects. Use "new" to create naManager (as mentioned earlier, QNetwork is asynchronous, and when QNetworkAccessManager exits the function without blocking, it will be destroyed). Then, connect the finished(QNetworkReply*) signal of naManager to the slot function requestFinished(QNetworkReply*). Set the URL of the request and create the reply object to get the response of the GET request.

void requestFinished(QNetworkReply* reply) {
	QString Result;
	// Get the HTTP status code
	QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
	if (statusCode.isValid())
		Result+=QString("status code=%1\n").arg(statusCode.toInt());

	QVariant reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
	if (reason.isValid())
		Result+=QString("reason=%1\n").arg(reason.toString());

	QNetworkReply::NetworkError err = reply->error();
	if (err != QNetworkReply::NoError) {
		Result+=QString("Failed: %1\n").arg(reply->errorString());
	}
	else {
		// Get the response content
		Result+= reply->readAll();
	}
	ui.Result->setText(Result);
}

Previously, we used the reply object to get the response result. Here, we parse the response.

Reference article: https://blog.csdn.net/china_jeffery/article/details/83246355

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.