QNetworkAccessManager 類

QNetworkAccessManager 類允許應用程序發送網絡請求和接收迴復。 更多...

頭: #include <QNetworkAccessManager>
qmake: QT += network
Since: Qt 4.4
繼承: QObject

該類在 Qt 4.4 引入。

注意: 此類的所有函數 可重入 .

公共類型

enum 操作 { HeadOperation, GetOperation, PutOperation, PostOperation, DeleteOperation, CustomOperation }

公共函數

QNetworkAccessManager (QObject * parent = nullptr)
virtual ~QNetworkAccessManager ()
void addStrictTransportSecurityHosts (const QVector<QHstsPolicy> & knownHosts )
bool autoDeleteReplies () const
QAbstractNetworkCache * cache () const
void clearAccessCache ()
void clearConnectionCache ()
void connectToHost (const QString & hostName , quint16 port = 80)
void connectToHostEncrypted (const QString & hostName , quint16 port = 443, const QSslConfiguration & sslConfiguration = QSslConfiguration::defaultConfiguration())
void connectToHostEncrypted (const QString & hostName , quint16 port , const QSslConfiguration & sslConfiguration , const QString & peerName )
QNetworkCookieJar * cookieJar () const
QNetworkReply * deleteResource (const QNetworkRequest & request )
void enableStrictTransportSecurityStore (bool enabled , const QString & storeDir = QString())
QNetworkReply * get (const QNetworkRequest & request )
QNetworkReply * head (const QNetworkRequest & request )
bool isStrictTransportSecurityEnabled () const
bool isStrictTransportSecurityStoreEnabled () const
QNetworkReply * post (const QNetworkRequest & request , QIODevice * data )
QNetworkReply * post (const QNetworkRequest & request , const QByteArray & data )
QNetworkReply * post (const QNetworkRequest & request , QHttpMultiPart * multiPart )
QNetworkProxy proxy () const
QNetworkProxyFactory * proxyFactory () const
QNetworkReply * put (const QNetworkRequest & request , QIODevice * data )
QNetworkReply * put (const QNetworkRequest & request , const QByteArray & data )
QNetworkReply * put (const QNetworkRequest & request , QHttpMultiPart * multiPart )
QNetworkRequest::RedirectPolicy redirectPolicy () const
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QIODevice * data = nullptr)
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , const QByteArray & data )
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QHttpMultiPart * multiPart )
void setAutoDeleteReplies (bool shouldAutoDelete )
void setCache (QAbstractNetworkCache * cache )
void setCookieJar (QNetworkCookieJar * cookieJar )
void setProxy (const QNetworkProxy & proxy )
void setProxyFactory (QNetworkProxyFactory * factory )
void setRedirectPolicy (QNetworkRequest::RedirectPolicy policy )
void setStrictTransportSecurityEnabled (bool enabled )
void setTransferTimeout (int timeout = QNetworkRequest::DefaultTransferTimeoutConstant)
QVector<QHstsPolicy> strictTransportSecurityHosts () const
QStringList supportedSchemes () const
int transferTimeout () const

信號

void authenticationRequired (QNetworkReply * reply , QAuthenticator * authenticator )
void encrypted (QNetworkReply * reply )
void finished (QNetworkReply * reply )
void preSharedKeyAuthenticationRequired (QNetworkReply * reply , QSslPreSharedKeyAuthenticator * authenticator )
void proxyAuthenticationRequired (const QNetworkProxy & proxy , QAuthenticator * authenticator )
void sslErrors (QNetworkReply * reply , const QList<QSslError> & errors )

保護函數

virtual QNetworkReply * createRequest (QNetworkAccessManager::Operation op , const QNetworkRequest & originalReq , QIODevice * outgoingData = nullptr)

保護槽

QStringList supportedSchemesImplementation () const

詳細描述

網絡訪問 API 圍繞某一 QNetworkAccessManager 對象構造,其保持請求它發送的公共配置和設置。它包含代理 緩存配置、這種問題的相關信號、及可以用於監視網絡操作進度的迴復信號。一個 QNetworkAccessManager 實例對於整個 Qt 應用程序,應該是足夠的。因為 QNetworkAccessManager 基於 QObject ,它隻能在其所屬的綫程中使用。

一旦創建 QNetworkAccessManager 對象,應用程序就可以通過網絡使用它發送請求。提供一組接受請求和可選數據的標準函數,且各函數返迴 QNetworkReply 對象。返迴對象用於獲取響應相應請求,返迴的任何數據。

施行簡單下載網絡斷開,可以采用:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished,
        this, &MyClass::replyFinished);
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
					

QNetworkAccessManager 擁有異步 API。當 replyFinished 槽被調用時,它接受的參數是 QNetworkReply 對象,包含下載數據及元數據 (Header 頭、等)。

注意: 在請求完成後,用戶有責任刪除 QNetworkReply 對象,在適當時。不要直接在槽內刪除它,因為槽已連接到 finished ()。可以使用 deleteLater () 函數。

注意: QNetworkAccessManager 將它收到的請求排隊。並行執行的請求數從屬協議。目前,對於桌麵平颱 HTTP 協議,一個主機/端口的組閤可並行執行 6 個請求。

假定管理器已存在,更多涉及範例可以是:

QNetworkRequest request;
request.setUrl(QUrl("http://qt-project.org"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
QNetworkReply *reply = manager->get(request);
connect(reply, &QIODevice::readyRead, this, &MyClass::slotReadyRead);
connect(reply, &QNetworkReply::errorOccurred,
        this, &MyClass::slotError);
connect(reply, &QNetworkReply::sslErrors,
        this, &MyClass::slotSslErrors);
					

另請參閱 QNetworkRequest , QNetworkReply ,和 QNetworkProxy .

成員類型文檔編製

enum QNetworkAccessManager:: 操作

指示此迴復正在處理的操作。

常量 描述
QNetworkAccessManager::HeadOperation 1 檢索 Header (頭) 操作 (創建采用 head ())
QNetworkAccessManager::GetOperation 2 檢索 Header (頭) 並下載內容 (創建采用 get ())
QNetworkAccessManager::PutOperation 3 上傳內容操作 (創建采用 put ())
QNetworkAccessManager::PostOperation 4 經由 HTTP POST 發送用於處理的 HTML 錶單內容 (創建采用 post ())
QNetworkAccessManager::DeleteOperation 5 刪除內容操作 (創建采用 deleteResource ())
QNetworkAccessManager::CustomOperation 6 自定義操作 (創建采用 sendCustomRequest ())

該枚舉在 Qt 4.7 引入或被修改。

另請參閱 QNetworkReply::operation ().

成員函數文檔編製

QNetworkAccessManager:: QNetworkAccessManager ( QObject * parent = nullptr)

構造 QNetworkAccessManager 對象 (網絡訪問 API 的中心) 並設置 parent 作為父級對象。

[signal] void QNetworkAccessManager:: authenticationRequired ( QNetworkReply * reply , QAuthenticator * authenticator )

此信號被發射每當最終服務器請求身份驗證,在交付請求內容之前。連接到此信號的槽應為內容填充證書(可以確定通過審查 reply 對象) 在 authenticator 對象。

QNetworkAccessManager 會在內部緩存證書並發送相同值,若服務器再次要求身份驗證,而不發射 authenticationRequired() 信號。若它拒絕證書,此信號會被再次發射。

注意: 要讓請求不發送證書,必須不調用 setUser() 或 setPassword() 在 authenticator 對象。這將導緻 finished () 信號被發射采用 QNetworkReply 采用錯誤 AuthenticationRequiredError .

注意: 使用 QueuedConnection 去連接到此信號是不可能的,因為連接會失敗,若身份驗證器沒有采新信息被填充,當信號返迴時。

另請參閱 proxyAuthenticationRequired (), QAuthenticator::setUser (),和 QAuthenticator::setPassword ().

[signal] void QNetworkAccessManager:: encrypted ( QNetworkReply * reply )

此信號被發射當 SSL/TLS 會話已成功完成初始握手時。此時,尚未傳輸用戶數據。可以使用信號以履行額外證書鏈校驗 (例如:通知用戶當網站證書改變時)。 reply 參數指定哪個負責網絡迴復。若迴復不匹配期望準則,則應中止它通過調用 QNetworkReply::abort () 通過連接到此信號的槽。可以審查在使用中的 SSL 配置,使用 QNetworkReply::sslConfiguration () 方法。

在內部, QNetworkAccessManager 可能打開到服務器的多個連接,以允許它並行處理請求。這些連接可以被重用,意味著不會發射 encrypted() 信號。這意味著隻能保證接收此信號 (對於首次連接到站點而言),在使用期內為 QNetworkAccessManager .

該函數在 Qt 5.1 引入。

另請參閱 QSslSocket::encrypted () 和 QNetworkReply::encrypted ().

[signal] void QNetworkAccessManager:: finished ( QNetworkReply * reply )

此信號被發射每當待決網絡迴復完成時。 reply 參數將包含指嚮剛剛完成的迴復的指針。此信號被串聯發射采用 QNetworkReply::finished () 信號。

QNetworkReply::finished () 瞭解對象將處於何種狀態的有關信息。

注意: 不要刪除 reply 對象在連接到此信號的槽中。使用 deleteLater ().

另請參閱 QNetworkReply::finished () 和 QNetworkReply::error ().

[signal] void QNetworkAccessManager:: preSharedKeyAuthenticationRequired ( QNetworkReply * reply , QSslPreSharedKeyAuthenticator * authenticator )

This signal is emitted if the SSL/TLS handshake negotiates a PSK ciphersuite, and therefore a PSK authentication is then required. The reply 對象是 QNetworkReply that is negotiating such ciphersuites.

當使用 PSK 時,客戶端必須嚮服務器發送有效標識和有效 PSK (預共享密鑰) 以便 SSL 握手得以繼續。應用程序可以在此信號連接的槽中提供此信息,通過填入傳遞的 authenticator 對象根據需要。

注意: 忽略此信號或未能提供要求證書,將導緻握手失敗,因此連接將被中止。

注意: The authenticator 對象由迴復所擁有,且必須不可以通過應用程序被刪除。

該函數在 Qt 5.5 引入。

另請參閱 QSslPreSharedKeyAuthenticator .

[signal] void QNetworkAccessManager:: proxyAuthenticationRequired (const QNetworkProxy & proxy , QAuthenticator * authenticator )

此信號被發射每當代理請求身份驗證和 QNetworkAccessManager cannot find a valid, cached credential. The slot connected to this signal should fill in the credentials for the proxy proxy authenticator 對象。

QNetworkAccessManager will cache the credentials internally. The next time the proxy requests authentication, QNetworkAccessManager will automatically send the same credential without emitting the proxyAuthenticationRequired signal again.

若代理拒絕證書, QNetworkAccessManager 會再次發射信號。

另請參閱 proxy (), setProxy (),和 authenticationRequired ().

[signal] void QNetworkAccessManager:: sslErrors ( QNetworkReply * reply , const QList < QSslError > & errors )

此信號被發射,若 SSL/TLS 會話在設置期間遇到錯誤 (包括證書驗證錯誤)。 errors parameter contains the list of errors and reply QNetworkReply that is encountering these errors.

為指示錯誤不緻命,且連接應繼續進行, QNetworkReply::ignoreSslErrors () 函數應該被調用,從連接到此信號的槽中。若不調用,SSL 會話將被斷開,在交換任何數據 (包括 URL) 之前。

This signal can be used to display an error message to the user indicating that security may be compromised and display the SSL settings (see sslConfiguration() to obtain it). If the user decides to proceed after analyzing the remote certificate, the slot should call ignoreSslErrors().

另請參閱 QSslSocket::sslErrors (), QNetworkReply::sslErrors (), QNetworkReply::sslConfiguration (),和 QNetworkReply::ignoreSslErrors ().

[protected slot] QStringList QNetworkAccessManager:: supportedSchemesImplementation () const

列齣由訪問管理器支持的所有 URL 方案。

您不應該直接調用此函數。使用 QNetworkAccessManager::supportedSchemes () 代替。

Reimplement this slot to provide your own supported schemes in a QNetworkAccessManager subclass. It is for instance necessary when your subclass provides support for new protocols.

Because of binary compatibility constraints, the supportedSchemes () method (introduced in Qt 5.2) is not virtual. Instead, supportedSchemes () will dynamically detect and call this slot.

該函數在 Qt 5.2 引入。

另請參閱 supportedSchemes ().

[虛擬] QNetworkAccessManager:: ~QNetworkAccessManager ()

銷毀 QNetworkAccessManager 對象並釋放任何資源。注意 QNetworkReply objects that are returned from this class have this object set as their parents, which means that they will be deleted along with it if you don't call QObject::setParent () on them.

void QNetworkAccessManager:: addStrictTransportSecurityHosts (const QVector < QHstsPolicy > & knownHosts )

將 HSTS (HTTP 嚴格傳輸安全) 策略添加到 HSTS 緩存。 knownHosts contains the known hosts that have QHstsPolicy information.

注意: An expired policy will remove a known host from the cache, if previously present.

注意: 當處理 HTTP 響應, QNetworkAccessManager can also update the HSTS cache, removing or updating exitsting policies or introducing new knownHosts . The current implementation thus is server-driven, client code can provide QNetworkAccessManager with previously known or discovered policies, but this information can be overridden by "Strict-Transport-Security" response headers.

該函數在 Qt 5.9 引入。

另請參閱 addStrictTransportSecurityHosts(), enableStrictTransportSecurityStore (),和 QHstsPolicy .

bool QNetworkAccessManager:: autoDeleteReplies () const

返迴 true 若 QNetworkAccessManager 目前被配置為自動刪除 QNetworkReplies,否則 false。

該函數在 Qt 5.14 引入。

另請參閱 setAutoDeleteReplies and QNetworkRequest::AutoDeleteReplyOnFinishAttribute .

QAbstractNetworkCache *QNetworkAccessManager:: cache () const

返迴用於存儲從網絡獲得數據的緩存。

該函數在 Qt 4.5 引入。

另請參閱 setCache ().

void QNetworkAccessManager:: clearAccessCache ()

刷新身份驗證數據和網絡連接的內部緩存。

此函數對履行自動測試很有用。

該函數在 Qt 5.0 引入。

另請參閱 clearConnectionCache ().

void QNetworkAccessManager:: clearConnectionCache ()

刷新網絡連接的內部緩存。相比 clearAccessCache () 保留身份驗證數據。

該函數在 Qt 5.9 引入。

另請參閱 clearAccessCache ().

void QNetworkAccessManager:: connectToHost (const QString & hostName , quint16 port = 80)

初啓連接到主機,給定通過 hostName 在端口 port 。此函數對在發齣 HTTP 請求之前完成與主機的 TCP 握手很有用,結果是降低網絡延遲。

注意: 此函數沒有可能去報告錯誤。

該函數在 Qt 5.2 引入。

另請參閱 connectToHostEncrypted (), get (), post (), put (),和 deleteResource ().

void QNetworkAccessManager:: connectToHostEncrypted (const QString & hostName , quint16 port = 443, const QSslConfiguration & sslConfiguration = QSslConfiguration::defaultConfiguration())

初啓連接到主機,給定通過 hostName 在端口 port ,使用 sslConfiguration . This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.

注意: Preconnecting a SPDY connection can be done by calling setAllowedNextProtocols() on sslConfiguration with QSslConfiguration::NextProtocolSpdy3_0 contained in the list of allowed protocols. When using SPDY, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.

注意: 此函數沒有可能去報告錯誤。

該函數在 Qt 5.2 引入。

另請參閱 connectToHost (), get (), post (), put (),和 deleteResource ().

void QNetworkAccessManager:: connectToHostEncrypted (const QString & hostName , quint16 port , const QSslConfiguration & sslConfiguration , const QString & peerName )

這是重載函數。

初啓連接到主機,給定通過 hostName 在端口 port ,使用 sslConfiguration with peerName set to be the hostName used for certificate validation. This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.

注意: Preconnecting a SPDY connection can be done by calling setAllowedNextProtocols() on sslConfiguration with QSslConfiguration::NextProtocolSpdy3_0 contained in the list of allowed protocols. When using SPDY, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.

注意: 此函數沒有可能去報告錯誤。

該函數在 Qt 5.13 引入。

另請參閱 connectToHost (), get (), post (), put (),和 deleteResource ().

QNetworkCookieJar *QNetworkAccessManager:: cookieJar () const

返迴 QNetworkCookieJar 用於存儲從網絡獲得的 Cookie 及即將被發送的 Cookie。

另請參閱 setCookieJar ().

[virtual protected] QNetworkReply *QNetworkAccessManager:: createRequest ( QNetworkAccessManager::Operation op , const QNetworkRequest & originalReq , QIODevice * outgoingData = nullptr)

返迴新的 QNetworkReply 對象以處理操作 op 和請求 originalReq 。設備 outgoingData 始終為 0 對於 Get 和 Head 請求而言,但是值被傳遞給 post () 和 put () 在這些操作中 ( QByteArray 變體會傳遞 QBuffer 對象)。

默認實現調用 QNetworkCookieJar::cookiesForUrl () 當 Cookie Jar 設置采用 setCookieJar () 以獲得要被發送給遠程服務器的 Cookie。

返迴的對象必須處於打開狀態。

QNetworkReply *QNetworkAccessManager:: deleteResource (const QNetworkRequest & request )

發送請求以刪除資源標識通過 URL 的 request .

注意: 此特徵目前隻可用於 HTTP,履行 HTTP DELETE 請求。

該函數在 Qt 4.6 引入。

另請參閱 get (), post (), put (),和 sendCustomRequest ().

void QNetworkAccessManager:: enableStrictTransportSecurityStore ( bool enabled , const QString & storeDir = QString())

enabled is true ,內部 HSTS (HTTP 嚴格傳輸安全) 緩存將使用持久存儲來讀寫 HSTS 策略。 storeDir defines where this store will be located. The default location is defined by QStandardPaths::CacheLocation . If there is no writable QStandartPaths::CacheLocation and storeDir is an empty string, the store will be located in the program's working directory.

注意: If HSTS cache already contains HSTS policies by the time persistent store is enabled, these policies will be preserved in the store. In case both cache and store contain the same known hosts, policies from cache are considered to be more up-to-date (and thus will overwrite the previous values in the store). If this behavior is undesired, enable HSTS store before enabling Strict Transport Security. By default, the persistent store of HSTS policies is disabled.

該函數在 Qt 5.10 引入。

另請參閱 isStrictTransportSecurityStoreEnabled (), setStrictTransportSecurityEnabled (),和 QStandardPaths::standardLocations ().

QNetworkReply *QNetworkAccessManager:: get (const QNetworkRequest & request )

張貼請求以獲取內容對於目標 request 並返迴新的 QNetworkReply 對象並打開為讀取發射的 readyRead() 信號,每當新數據到達時。

會下載內容及關聯 Header (頭)。

另請參閱 post (), put (), deleteResource (),和 sendCustomRequest ().

張貼請求以獲取網絡頭為 request 並返迴新的 QNetworkReply 對象 (將包含這種 Header 頭)。

函數以 HTTP 請求關聯 HEAD 頭命名。

bool QNetworkAccessManager:: isStrictTransportSecurityEnabled () const

返迴 true,若 HSTS (HTTP 嚴格傳輸安全) 被啓用。默認情況下,HSTS 是禁用的。

該函數在 Qt 5.9 引入。

另請參閱 setStrictTransportSecurityEnabled ().

bool QNetworkAccessManager:: isStrictTransportSecurityStoreEnabled () const

返迴 true,若 HSTS (HTTP 嚴格傳輸安全) 緩存使用永久存儲去加載、存儲 HSTS 策略。

該函數在 Qt 5.10 引入。

另請參閱 enableStrictTransportSecurityStore ().

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , QIODevice * data )

把 HTTP POST (張貼) 請求發送給指定目的地通過 request 並返迴新的 QNetworkReply 為讀取而打開的對象,將包含由服務器所發送的迴復。內容對於 data 設備將上傳到服務器。

data 必須被打開以供讀取且必須保持有效,直到 finished () 信號被發射為此迴復。

注意: 發送除 HTTP HTTPS 協議的 POST (張貼) 請求是未定義的,且可能失敗。

另請參閱 get (), put (), deleteResource (),和 sendCustomRequest ().

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , const QByteArray & data )

這是重載函數。

發送內容為 data 字節數組到目的地指定通過 request .

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , QHttpMultiPart * multiPart )

這是重載函數。

發送內容為 multiPart 消息到目的地指定通過 request .

這可以被用於通過 HTTP 發送 MIME 多部分消息。

該函數在 Qt 4.8 引入。

另請參閱 QHttpMultiPart , QHttpPart ,和 put ().

QNetworkProxy QNetworkAccessManager:: proxy () const

返迴 QNetworkProxy 請求發送使用此 QNetworkAccessManager 對象將使用。代理的默認值為 QNetworkProxy::DefaultProxy .

另請參閱 setProxy (), setProxyFactory (),和 proxyAuthenticationRequired ().

QNetworkProxyFactory *QNetworkAccessManager:: proxyFactory () const

返迴代理工廠,此 QNetworkAccessManager 對象被用於確定要被用於請求的代理。

注意:由此函數返迴的指針的管理是通過 QNetworkAccessManager 且可以在任何時候被刪除。

該函數在 Qt 4.5 引入。

另請參閱 setProxyFactory () 和 proxy ().

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , QIODevice * data )

上傳內容為 data 到目的地 request 並返迴新的 QNetworkReply 對象會被打開為迴復。

data 必須被打開以供讀取,當此函數被調用時;且必須保持有效直到 finished () 信號被發射為此迴復。

返迴對象是否有任何東西可供讀取,取決於協議。對於 HTTP,服務器可能發送指示上傳成功 (或不成功) 的小 HTML 頁麵。其它協議可能會在其迴復中有內容。

注意: 對於 HTTP,此請求將發送 PUT 請求 (大多數服務器不允許)。錶單上傳機製,包括透過 HTML 錶單上傳文件的機製,使用 POST 機製。

另請參閱 get (), post (), deleteResource (),和 sendCustomRequest ().

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , const QByteArray & data )

這是重載函數。

發送內容為 data 字節數組到目的地指定通過 request .

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , QHttpMultiPart * multiPart )

這是重載函數。

發送內容為 multiPart 消息到目的地指定通過 request .

這可以被用於通過 HTTP 發送 MIME 多部分消息。

該函數在 Qt 4.8 引入。

另請參閱 QHttpMultiPart , QHttpPart ,和 post ().

QNetworkRequest::RedirectPolicy QNetworkAccessManager:: redirectPolicy () const

返迴所用的重定嚮策略,當創建新請求時。

該函數在 Qt 5.9 引入。

另請參閱 setRedirectPolicy () 和 QNetworkRequest::RedirectPolicy .

QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QIODevice * data = nullptr)

嚮服務器發送自定義請求標識通過 URL 的 request .

用戶負責發送 verb 給根據 HTTP 規範有效的服務器。

This method provides means to send verbs other than the common ones provided via get () 或 post () etc., for instance sending an HTTP OPTIONS command.

data is not empty, the contents of the data device will be uploaded to the server; in that case, data must be open for reading and must remain valid until the finished () 信號被發射為此迴復。

注意: 此特徵目前隻可用於 HTTP(S)。

該函數在 Qt 4.7 引入。

另請參閱 get (), post (), put (),和 deleteResource ().

QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , const QByteArray & data )

這是重載函數。

發送內容為 data 字節數組到目的地指定通過 request .

該函數在 Qt 5.8 引入。

QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QHttpMultiPart * multiPart )

這是重載函數。

嚮服務器發送自定義請求標識通過 URL 的 request .

發送內容為 multiPart 消息到目的地指定通過 request .

這可用於為自定義 verb 發送 MIME 多部分消息。

該函數在 Qt 5.8 引入。

另請參閱 QHttpMultiPart , QHttpPart ,和 put ().

void QNetworkAccessManager:: setAutoDeleteReplies ( bool shouldAutoDelete )

啓用 (或禁用) 自動刪除 QNetworkReplies .

設置 shouldAutoDelete to true is the same as setting the QNetworkRequest::AutoDeleteReplyOnFinishAttribute attribute to true on all future QNetworkRequests passed to this instance of QNetworkAccessManager unless the attribute was already explicitly set on the QNetworkRequest .

該函數在 Qt 5.14 引入。

另請參閱 autoDeleteReplies and QNetworkRequest::AutoDeleteReplyOnFinishAttribute .

void QNetworkAccessManager:: setCache ( QAbstractNetworkCache * cache )

將管理器的網絡緩存設為 cache 指定。緩存用於由管理器分派的所有請求。

Use this function to set the network cache object to a class that implements additional features, like saving the cookies to permanent storage.

注意: QNetworkAccessManager 擁有所有權對於 cache 對象。

QNetworkAccessManager by default does not have a set cache. Qt provides a simple disk cache, QNetworkDiskCache , which can be used.

該函數在 Qt 4.5 引入。

另請參閱 cache () 和 QNetworkRequest::CacheLoadControl .

void QNetworkAccessManager:: setCookieJar ( QNetworkCookieJar * cookieJar )

將管理器的 Cookie Jar 設為 cookieJar 指定。Cookie Jar 用於由管理器分派的所有請求。

Use this function to set the cookie jar object to a class that implements additional features, like saving the cookies to permanent storage.

注意: QNetworkAccessManager 擁有所有權對於 cookieJar 對象。

cookieJar is in the same thread as this QNetworkAccessManager , it will set the parent of the cookieJar so that the cookie jar is deleted when this object is deleted as well. If you want to share cookie jars between different QNetworkAccessManager objects, you may want to set the cookie jar's parent to 0 after calling this function.

QNetworkAccessManager by default does not implement any cookie policy of its own: it accepts all cookies sent by the server, as long as they are well formed and meet the minimum security requirements (cookie domain matches the request's and cookie path matches the request's). In order to implement your own security policy, override the QNetworkCookieJar::cookiesForUrl () 和 QNetworkCookieJar::setCookiesFromUrl () virtual functions. Those functions are called by QNetworkAccessManager when it detects a new cookie.

另請參閱 cookieJar (), QNetworkCookieJar::cookiesForUrl (),和 QNetworkCookieJar::setCookiesFromUrl ().

void QNetworkAccessManager:: setProxy (const QNetworkProxy & proxy )

將用於未來請求的代理設為 proxy 。這不會影響已經發送的請求。 proxyAuthenticationRequired () 信號會被發射若代理請求身份驗證。

A proxy set with this function will be used for all requests issued by QNetworkAccessManager . In some cases, it might be necessary to select different proxies depending on the type of request being sent or the destination host. If that's the case, you should consider using setProxyFactory ().

另請參閱 proxy () 和 proxyAuthenticationRequired ().

void QNetworkAccessManager:: setProxyFactory ( QNetworkProxyFactory * factory )

將此類的代理工廠設為 factory . A proxy factory is used to determine a more specific list of proxies to be used for a given request, instead of trying to use the same proxy value for all requests.

All queries sent by QNetworkAccessManager will have type QNetworkProxyQuery::UrlRequest .

例如,代理工廠可以應用以下規則:

  • if the target address is in the local network (for example, if the hostname contains no dots or if it's an IP address in the organization's range), return QNetworkProxy::NoProxy
  • 若請求為 FTP,返迴 FTP 代理
  • 若請求為 HTTP 或 HTTPS,返迴 HTTP 代理
  • 否則,返迴 SOCKSv5 代理服務器

The lifetime of the object factory will be managed by QNetworkAccessManager 。它會刪除對象,當有必要時。

注意: 若指定代理的設置是采用 setProxy (),工廠將不會被使用。

該函數在 Qt 4.5 引入。

另請參閱 proxyFactory (), setProxy (),和 QNetworkProxyQuery .

void QNetworkAccessManager:: setRedirectPolicy ( QNetworkRequest::RedirectPolicy policy )

Sets the manager's redirect policy to be the policy specified. This policy will affect all subsequent requests created by the manager.

使用此函數在管理器級彆啓用或禁用 HTTP 重定嚮。

注意: 當創建請求時,QNetworkRequest::RedirectAttributePolicy 擁有最高優先級,下一優先級是 QNetworkRequest::FollowRedirectsAttribute 。最後,管理器的策略擁有最低優先級。

為嚮後兼容,默認值為 QNetworkRequest::ManualRedirectPolicy 。未來可能改變,且某些類型的自動重定嚮策略將變為默認;鼓勵依賴手動處理重定嚮的客戶端,在其代碼中明確設置此策略。

該函數在 Qt 5.9 引入。

另請參閱 redirectPolicy (), QNetworkRequest::RedirectPolicy ,和 QNetworkRequest::FollowRedirectsAttribute .

void QNetworkAccessManager:: setStrictTransportSecurityEnabled ( bool enabled )

enabled is true , QNetworkAccessManager follows the HTTP Strict Transport Security policy (HSTS, RFC6797). When processing a request, QNetworkAccessManager automatically replaces the "http" scheme with "https" and uses a secure transport for HSTS hosts. If it's set explicitly, port 80 is replaced by port 443.

When HSTS is enabled, for each HTTP response containing HSTS header and received over a secure transport, QNetworkAccessManager will update its HSTS cache, either remembering a host with a valid policy or removing a host with an expired or disabled HSTS policy.

該函數在 Qt 5.9 引入。

另請參閱 isStrictTransportSecurityEnabled ().

void QNetworkAccessManager:: setTransferTimeout ( int timeout = QNetworkRequest::DefaultTransferTimeoutConstant)

設置 timeout 作為傳輸超時 (以毫秒為單位)。

Transfers are aborted if no bytes are transferred before the timeout expires. Zero means no timer is set. If no argument is provided, the timeout is QNetworkRequest::DefaultTransferTimeoutConstant . If this function is not called, the timeout is disabled and has the value zero. The request-specific non-zero timeouts set for the requests that are executed override this value. This means that if QNetworkAccessManager has an enabled timeout, it needs to be disabled to execute a request without a timeout.

該函數在 Qt 5.15 引入。

另請參閱 transferTimeout ().

QVector < QHstsPolicy > QNetworkAccessManager:: strictTransportSecurityHosts () const

Returns the list of HTTP Strict Transport Security policies. This list can differ from what was initially set via addStrictTransportSecurityHosts () if HSTS cache was updated from a "Strict-Transport-Security" response header.

該函數在 Qt 5.9 引入。

另請參閱 addStrictTransportSecurityHosts () 和 QHstsPolicy .

QStringList QNetworkAccessManager:: supportedSchemes () const

列齣由訪問管理器支持的所有 URL 方案。

該函數在 Qt 5.2 引入。

另請參閱 supportedSchemesImplementation ().

int QNetworkAccessManager:: transferTimeout () const

返迴用於傳輸的超時 (以毫秒為單位)。

此超時為 0 若 setTransferTimeout () 沒有被調用,意味著超時不使用。

該函數在 Qt 5.15 引入。

另請參閱 setTransferTimeout ().