[Qt first entered the rivers and lakes] Detailed description of the underlying architecture and principles of Qt QNetworkRequest

Yuxian: CSDN content partner, CSDN new star mentor, 51CTO (Top celebrity + expert blogger), github open source enthusiast (secondary development of go-zero source code, game back-end architecture https://github.com/Peakchen)

                ​​​​​​​        ​​​​​​​    

 

Qt QNetworkRequest is a class used to describe a network request, which includes information such as the requested URL, request method, request header, and request body. The underlying architecture of QNetworkRequest consists of the following parts:

  1. QURL

QUrl is a class used to process URLs in Qt. It can parse URL strings into various parts, such as protocol, host name, port number, path, query parameters, fragments, etc., and provides some methods to get and set these parts . In QNetworkRequest, we can use QUrl to represent the URL of the request, and use the method of QUrl to set and get various parts of the URL.

  1. QNetworkRequestHeader

QNetworkRequestHeader is a class used in Qt to process HTTP request headers. It can add, delete, obtain and modify various fields of HTTP request headers. In QNetworkRequest, we can use QNetworkRequestHeader to represent the request header, and set and get the various fields of the request header through the method of QNetworkRequestHeader.

  1. QByteArray

QByteArray is a class used to process byte arrays in Qt. It can store arbitrary binary data and provides some methods to access and modify data. In QNetworkRequest, we can use QByteArray to represent the request body, and use the method of QByteArray to set and get the data of the request body.

The following is the underlying architecture diagram of QNetworkRequest:

In QNetworkRequest, we can use the following methods to set and get the request URL, request header and request body:

  1. setUrl()

The setUrl() method is used to set the requested URL, and it accepts a parameter of type QUrl, which is used to represent the requested URL. For example:

QUrl url("http://www.example.com");
QNetworkRequest request;
request.setUrl(url);
  1. url()

The url() method is used to obtain the requested URL, and it returns a value of type QUrl, representing the requested URL. For example:

QNetworkRequest request;
QUrl url = request.url();
  1. setHeader()

The setHeader() method is used to set the field of the request header. It accepts two parameters. The first parameter is an enumeration value indicating the field type of the request header, and the second parameter is a value of type QVariant indicating the request header. field value. For example:

QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
  1. header()

The header() method is used to obtain the field value of the request header. It accepts an enumeration value as a parameter, indicating the field type of the request header, and returns a value of type QVariant, indicating the field value of the request header. For example:

QNetworkRequest request;
QString content_type = request.header(QNetworkRequest::ContentTypeHeader).toString();
  1. setRawHeader()

The setRawHeader() method is used to set the raw byte data of the request header. It accepts two parameters. The first parameter is a byte array representing the field name of the request header, and the second parameter is a byte array representing the request header field value. For example:

QNetworkRequest request;
request.setRawHeader("Authorization", "Bearer xxxxxxxx");
  1. rawHeader()

The rawHeader() method is used to obtain the original byte data of the request header. It accepts a byte array as a parameter, indicating the field name of the request header, and returns a byte array, indicating the field value of the request header. For example:

QNetworkRequest request;
QByteArray auth_header = request.rawHeader("Authorization");
  1. setRawHeaderList()

The setRawHeaderList() method is used to set the raw byte data list of the request header, which accepts a parameter of type QList, indicating the raw byte data list of the request header. For example:

QNetworkRequest request;
QList<QPair<QByteArray, QByteArray>> headers;
headers << qMakePair(QByteArray("Content-Type"), QByteArray("application/json"));
headers << qMakePair(QByteArray("Authorization"), QByteArray("Bearer xxxxxxxx"));
request.setRawHeaderList(headers);
  1. rawHeaderList()

The rawHeaderList() method is used to obtain the raw byte data list of the request header, and returns a value of type QList, representing the raw byte data list of the request header. For example:

QNetworkRequest request;
QList<QPair<QByteArray, QByteArray>> headers = request.rawHeaderList();
  1. setAttribute()

The setAttribute() method is used to set the attribute of QNetworkRequest. It accepts two parameters. The first parameter is an enumeration value indicating the attribute type, and the second parameter is a value of type QVariant indicating the attribute value. For example:

QNetworkRequest request;
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
  1. attribute()

The attribute() method is used to obtain the attribute value of QNetworkRequest. It accepts an enumeration value as a parameter, indicating the attribute type, and returns a QVariant type value, indicating the attribute value. For example:

QNetworkRequest request;
bool follow_redirects = request.attribute(QNetworkRequest::FollowRedirectsAttribute).toBool();

Here is a sample code showing how to use QNetworkRequest to send an HTTP GET request:

#include <QtNetwork>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QUrl url("http://www.example.com");

    QNetworkRequest request;
    request.setUrl(url);

    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.get(request);

    QObject::connect(reply, &QNetworkReply::finished, [&]() {
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray data = reply->readAll();
            qDebug() << data;
        } else {
            qDebug() << "Error: " << reply->errorString();
        }

        app.quit();
    });

    return app.exec();
}

In the above code, we first created a QUrl object to represent the requested URL, then created a QNetworkRequest object to represent the request, and set the requested URL. Next, we created a QNetworkAccessManager object to manage network requests, and sent an HTTP GET request through the get() method of QNetworkAccessManager. Finally, we connect the finished signal of QNetworkReply through the QObject::connect() method, wait for the HTTP response to complete, and process the HTTP response data in the callback function.

Guess you like

Origin blog.csdn.net/feng1790291543/article/details/131806880