Qt Network 的变化

Qt 6 是努力使框架更高效,且更易于使用的结果。

为兼容每个发行的所有公共 API,我们试着维护二进制和源代码。但是,为使 Qt 成为更优框架,一些改变是不可避免的。

In this topic we summarize those changes in Qt Network, and provide guidance to handle them.

API 变化

Ambigous name overloads

Several ambigous overloaded functions are removed. The error() signal is replaced by errorOccured() in QAbstractSocket and its heirs ( QTcpSocket , QUdpSocket , QLocalSocket ,和 QSslSocket ), and in QNetworkReply . Code such as:

connect(socket, qOverload<QAbstractSocket::SocketError>(&QAbstractSocket::error),
        this, &SomeClass::errorSlot);
							

must therefore be changed to:

connect(socket, &QAbstractSocket::errorOccurred, this, &SomeClass::errorSlot);
							

QSslSocket , the function that returns a list of errors encountered during the TLS handshake:

QList<QSslError> sslErrors() const;
							

被重命名为 sslHandshakeErrors():

const auto tlsErrors = socket.sslHandshakeErrors();
							

Bearer management is removed

The classes QNetworkConfiguration and QNetworkConfigurationManager are removed in Qt 6. Consequently, the following member functions of QNetworkAccessManager are also removed:

void setConfiguration(const QNetworkConfiguration &config);
QNetworkConfiguration configuration() const;
QNetworkConfiguration activeConfiguration() const;
void setNetworkAccessible(NetworkAccessibility accessible);
NetworkAccessibility networkAccessible() const;
void networkSessionConnected();
							

注意: Qt 6.0 does not provide replacements for these deleted classes and functions.

被删除的枚举器

几个枚举器被移除在 QtNetwork . This includes constants for no longer supported protocols and functionality:

  • QSsl::SslV2;
  • QSsl::SslV3;
  • QSsl::TlsV1SslV3;
  • QNetworkRequest::SpdyAllowedAttribute;
  • QNetworkRequest::SpdyWasUsedAttribute;
  • QNetworkAccessManager::UnknownAccessibility;
  • QNetworkAccessManager::NotAccessible;
  • QNetworkAccessManager::Accessible

and enumerators whose names did not follow proper naming conventions:

QNetworkRequest::FollowRedirectsAttribute is removed in Qt 6, see the section about redirects handling below.

配置 QSslSocket

以下弃用功能在 Qt 6 中被移除:

QList<QSslCipher> ciphers() const;
void setCiphers(const QList<QSslCipher> &ciphers);
void setCiphers(const QString &ciphers);
static void setDefaultCiphers(const QList<QSslCipher> &ciphers);
static QList<QSslCipher> defaultCiphers();
static QList<QSslCipher> supportedCiphers();
QList<QSslCipher> ciphers() const;
void setCiphers(const QList<QSslCipher> &ciphers);
void setCiphers(const QString &ciphers);
static void setDefaultCiphers(const QList<QSslCipher> &ciphers);
static QList<QSslCipher> defaultCiphers();
static QList<QSslCipher> supportedCiphers();
bool addCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
                       QRegExp::PatternSyntax syntax = QRegExp::FixedString);
void addCaCertificate(const QSslCertificate &certificate);
void addCaCertificates(const QList<QSslCertificate> &certificates);
void setCaCertificates(const QList<QSslCertificate> &certificates);
QList<QSslCertificate> caCertificates() const;
static bool addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format = QSsl::Pem,
                                     QRegExp::PatternSyntax syntax = QRegExp::FixedString);
static void addDefaultCaCertificate(const QSslCertificate &certificate);
static void addDefaultCaCertificates(const QList<QSslCertificate> &certificates);
static void setDefaultCaCertificates(const QList<QSslCertificate> &certificates);
static QList<QSslCertificate> defaultCaCertificates();
static QList<QSslCertificate> systemCaCertificates();
							

使用 QSslConfiguration and its member functions to set these parameters, e.g.:

auto sslConfiguration = QSslConfiguration::defaultConfiguration();
sslConfiguration.setCiphers("ECDHE-ECDSA-AES256-SHA384");
// Set other parameters here ...
socket.setSslConfiguration(sslConfiguration);
							

Changes in QNetworkAccessManager's default behavior

重定向策略

In Qt 6, the default redirect policy has changed from manual to QNetworkRequest::NoLessSafeRedirectPolicy . If your application relies on manual redirect handling (it connects its slot to the QNetworkReply::redirected signal), you have to explicitly set this policy when creating a request:

request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
							

HTTP/2 默认是启用的若它被此代理使用。

在 Qt 6 中 QNetworkAccessManager enables HTTP/2 protocol by default. Depending on the scheme ("https" or "http"), QNetworkAccessManager will use the Application Layer Protocol Negotiation TLS extension or "protocol upgrade" HTTP header to negotiate HTTP/2. If HTTP/2 cannot be negotiated, the access manager will fall back to using HTTP/1.1. If your application can only use HTTP/1.1, you have to disable HTTP/2 manually on a new request:

request.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);