Qt Network 的變化

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

為兼容每個發行的所有公共 API,我們試著維護二進製和源代碼。但是,為使 Qt 成為更優框架,一些改變是不可避免的。

本主題匯總瞭 Qt Network 的這些變化,並提供處理它們的指導。

API 變化

歧義名稱重載

移除瞭幾個有歧義的重載函數。以 errorOccurred() 替換 error() 信號在 QAbstractSocket 及其繼承者 ( QTcpSocket , QUdpSocket , QLocalSocket ,和 QSslSocket ),和在 QNetworkReply 。代碼譬如:

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

因此必須改為:

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();
					

移除承載管理

類 QNetworkConfiguration 和 QNetworkConfigurationManager 在 Qt 6 被移除。因此,下列成員函數對於 QNetworkAccessManager 也被移除:

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

QNetworkInformation , initially introduced in Qt 6.1, aims to replace some aspects of the bearer management API. It works by providing a unified API which subscribes to changes to the network as notified by the operating system.

QNetworkInformation::reachability (), introduced in Qt 6.1, replaces the QNetworkAccessManager::networkAccessible() function, while adding more detailed information about the reachability of the network. See its documentation for more details.

在 Qt 6.2 QNetworkInformation 獲得檢測俘虜傳送門的能力。

在 Qt 6.3 QNetworkInformation 獲得 QNetworkInformation::transportMedium () 和 QNetworkInformation::isMetered ().

被刪除的枚舉器

幾個枚舉器被移除在 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 在 Qt 6 被移除,見 處理重定嚮的有關章節 下文。

配置 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);
					

QNetworkAccessManager 默認行為的變化

重定嚮策略

在 Qt 6,默認重定嚮策略已從手動改為 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 默認啓用 HTTP/2 協議。從屬方案 (https 或 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);
					

QNetworkAccessManager 現在守衛針對存檔炸彈

從 Qt 6.2 開始 QNetworkAccessManager will guard against compressed files that decompress to files which are much larger than their compressed form by erroring out the reply if the decompression ratio exceeds a certain threshold. This check is only applied to files larger than a certain size, which can be customized (or disabled by passing -1) by calling QNetworkRequest::setDecompressedSafetyCheckThreshold ().