The QNetworkAccessManager class allows the application to send network requests and receive replies. 更多...
| 頭: | #include <QNetworkAccessManager> |
| qmake: | QT += network |
| Since: | Qt 4.4 |
| 繼承: | QObject |
注意: 此類的所有函數 可重入 .
| enum | NetworkAccessibility { UnknownAccessibility, NotAccessible, Accessible } |
| enum | 操作 { HeadOperation, GetOperation, PutOperation, PostOperation, DeleteOperation, CustomOperation } |
| QNetworkAccessManager (QObject * parent = nullptr) | |
| virtual | ~QNetworkAccessManager () |
| QNetworkConfiguration | activeConfiguration () const |
| void | addStrictTransportSecurityHosts (const QVector<QHstsPolicy> & knownHosts ) |
| QAbstractNetworkCache * | cache () const |
| void | clearAccessCache () |
| void | clearConnectionCache () |
| QNetworkConfiguration | configuration () const |
| void | connectToHost (const QString & hostName , quint16 port = 80) |
| void | connectToHostEncrypted (const QString & hostName , quint16 port = 443, const QSslConfiguration & sslConfiguration = QSslConfiguration::defaultConfiguration()) |
| 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 |
| QNetworkAccessManager::NetworkAccessibility | networkAccessible () 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 | setCache (QAbstractNetworkCache * cache ) |
| void | setConfiguration (const QNetworkConfiguration & config ) |
| void | setCookieJar (QNetworkCookieJar * cookieJar ) |
| void | setNetworkAccessible (QNetworkAccessManager::NetworkAccessibility accessible ) |
| void | setProxy (const QNetworkProxy & proxy ) |
| void | setProxyFactory (QNetworkProxyFactory * factory ) |
| void | setRedirectPolicy (QNetworkRequest::RedirectPolicy policy ) |
| void | setStrictTransportSecurityEnabled (bool enabled ) |
| QVector<QHstsPolicy> | strictTransportSecurityHosts () const |
| QStringList | supportedSchemes () const |
| void | authenticationRequired (QNetworkReply * reply , QAuthenticator * authenticator ) |
| void | encrypted (QNetworkReply * reply ) |
| void | finished (QNetworkReply * reply ) |
| void | networkAccessibleChanged (QNetworkAccessManager::NetworkAccessibility accessible ) |
| void | preSharedKeyAuthenticationRequired (QNetworkReply * reply , QSslPreSharedKeyAuthenticator * authenticator ) |
| void | proxyAuthenticationRequired (const QNetworkProxy & proxy , QAuthenticator * authenticator ) |
| void | sslErrors (QNetworkReply * reply , const QList<QSslError> & errors ) |
| const QMetaObject | staticMetaObject |
| virtual QNetworkReply * | createRequest (QNetworkAccessManager::Operation op , const QNetworkRequest & originalReq , QIODevice * outgoingData = nullptr) |
| QStringList | supportedSchemesImplementation () const |
The QNetworkAccessManager class allows the application to send network requests and receive replies.
The Network Access API is constructed around one QNetworkAccessManager object, which holds the common configuration and settings for the requests it sends. It contains the proxy and cache configuration, as well as the signals related to such issues, and reply signals that can be used to monitor the progress of a network operation. One QNetworkAccessManager instance should be enough for the whole Qt application. Since QNetworkAccessManager is based on QObject ,它隻能在其所屬的綫程中使用。
一旦 QNetworkAccessManager object has been created, the application can use it to send requests over the network. A group of standard functions are supplied that take a request and optional data, and each return a QNetworkReply 對象。返迴對象用於獲取響應相應請求,返迴的任何數據。
施行簡單下載網絡斷開,可以采用:
QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished); manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
QNetworkAccessManager
has an asynchronous API. When the
replyFinished
槽被調用時,它接受的參數是
QNetworkReply
對象,包含下載數據及元數據 (Header 頭、等)。
注意: 在請求完成後,用戶有責任刪除 QNetworkReply 對象,在適當時。不要直接在槽內刪除它,因為槽已連接到 finished ()。可以使用 deleteLater () 函數。
注意: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.
假定管理器已存在,更多涉及範例可以是:
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, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &MyClass::slotError); connect(reply, &QNetworkReply::sslErrors, this, &MyClass::slotSslErrors);
With the addition of the 承載管理 API to Qt 4.7 QNetworkAccessManager gained the ability to manage network connections. QNetworkAccessManager can start the network interface if the device is offline and terminates the interface if the current process is the last one to use the uplink. Note that some platforms utilize grace periods from when the last application stops using a uplink until the system actually terminates the connectivity link. Roaming is equally transparent. Any queued/pending network requests are automatically transferred to the new access point.
Clients wanting to utilize this feature should not require any changes. In fact it is likely that existing platform specific connection code can simply be removed from the application.
注意: The network and roaming support in QNetworkAccessManager is conditional upon the platform supporting connection management. The QNetworkConfigurationManager::NetworkSessionRequired can be used to detect whether QNetworkAccessManager utilizes this feature.
另請參閱 QNetworkRequest , QNetworkReply ,和 QNetworkProxy .
Indicates whether the network is accessible via this network access manager.
| 常量 | 值 | 描述 |
|---|---|---|
QNetworkAccessManager::UnknownAccessibility
|
-1
|
網絡的可訪問性不能確定。 |
QNetworkAccessManager::NotAccessible
|
0
|
The network is not currently accessible, either because there is currently no network coverage or network access has been explicitly disabled by a call to setNetworkAccessible (). |
QNetworkAccessManager::Accessible
|
1
|
網絡是可訪問的。 |
另請參閱 networkAccessible .
指示此迴復正在處理的操作。
| 常量 | 值 | 描述 |
|---|---|---|
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 ().
This property holds whether the network is currently accessible via this network access manager.
If the network is not accessible the network access manager will not process any new network requests, all such requests will fail with an error. Requests with URLs with the file:// scheme will still be processed.
By default the value of this property reflects the physical state of the device. Applications may override it to disable all network requests via this network access manager by calling
networkAccessManager->setNetworkAccessible(QNetworkAccessManager::NotAccessible);
Network requests can be re-enabled again, and this property will resume to reflect the actual device state by calling
networkAccessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
注意: 調用 setNetworkAccessible () does not change the network state.
該特性在 Qt 4.7 引入。
訪問函數:
| QNetworkAccessManager::NetworkAccessibility | networkAccessible () const |
| void | setNetworkAccessible (QNetworkAccessManager::NetworkAccessibility accessible ) |
通知程序信號:
| void | networkAccessibleChanged (QNetworkAccessManager::NetworkAccessibility accessible ) |
構造 QNetworkAccessManager object that is the center of the Network Access API and sets parent 作為父級對象。
[虛擬]
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.
Returns the current active network configuration.
If the network configuration returned by configuration () is of type QNetworkConfiguration::ServiceNetwork this function will return the current active child network configuration of that configuration. Otherwise returns the same network configuration as configuration ().
Use this function to return the actual network configuration currently in use by the network session.
該函數在 Qt 4.7 引入。
另請參閱 configuration ().
將 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 .
[signal]
void
QNetworkAccessManager::
authenticationRequired
(
QNetworkReply
*
reply
,
QAuthenticator
*
authenticator
)
此信號被發射每當最終服務器請求身份驗證,在交付請求內容之前。連接到此信號的槽應為內容填充證書(可以確定通過審查 reply 對象) 在 authenticator 對象。
QNetworkAccessManager 會在內部緩存證書並發送相同值,若服務器再次要求身份驗證,而不發射 authenticationRequired() 信號。若它拒絕證書,此信號會被再次發射。
注意: 要讓請求不發送證書,必須不調用 setUser() 或 setPassword() 在 authenticator 對象。這將導緻 finished () 信號被發射采用 QNetworkReply 采用錯誤 AuthenticationRequiredError .
注意: 使用 QueuedConnection 去連接到此信號是不可能的,因為連接會失敗,若身份驗證器沒有采新信息被填充,當信號返迴時。
另請參閱 proxyAuthenticationRequired (), QAuthenticator::setUser (),和 QAuthenticator::setPassword ().
返迴用於存儲從網絡獲得數據的緩存。
該函數在 Qt 4.5 引入。
另請參閱 setCache ().
刷新身份驗證數據和網絡連接的內部緩存。
此函數對履行自動測試很有用。
該函數在 Qt 5.0 引入。
另請參閱 clearConnectionCache ().
刷新網絡連接的內部緩存。相比 clearAccessCache () 保留身份驗證數據。
該函數在 Qt 5.9 引入。
另請參閱 clearAccessCache ().
Returns the network configuration that will be used to create the network session which will be used when processing network requests.
該函數在 Qt 4.7 引入。
另請參閱 setConfiguration () 和 activeConfiguration ().
初啓連接到主機,給定通過 hostName 在端口 port 。此函數對在發齣 HTTP 請求之前完成與主機的 TCP 握手很有用,結果是降低網絡延遲。
注意: 此函數沒有可能去報告錯誤。
該函數在 Qt 5.2 引入。
另請參閱 connectToHostEncrypted (), get (), post (), put (),和 deleteResource ().
初啓連接到主機,給定通過 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 ().
返迴 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。
返迴的對象必須處於打開狀態。
發送請求以刪除資源標識通過 URL 的 request .
注意: 此特徵目前隻可用於 HTTP,履行 HTTP DELETE 請求。
該函數在 Qt 4.6 引入。
另請參閱 get (), post (), put (),和 sendCustomRequest ().
若
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 Tranport Security. By default, the persistent store of HSTS policies is disabled.
該函數在 Qt 5.10 引入。
另請參閱 isStrictTransportSecurityStoreEnabled (), setStrictTransportSecurityEnabled (),和 QStandardPaths::standardLocations ().
[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 ().
張貼請求以獲取內容對於目標 request 並返迴新的 QNetworkReply 對象並打開為讀取發射的 readyRead() 信號,每當新數據到達時。
會下載內容及關聯 Header (頭)。
另請參閱 post (), put (), deleteResource (),和 sendCustomRequest ().
張貼請求以獲取網絡頭為 request 並返迴新的 QNetworkReply 對象 (將包含這種 Header 頭)。
函數以 HTTP 請求關聯 HEAD 頭命名。
返迴 true,若 HSTS (HTTP 嚴格傳輸安全) 被啓用。默認情況下,HSTS 是禁用的。
該函數在 Qt 5.9 引入。
另請參閱 setStrictTransportSecurityEnabled ().
返迴 true,若 HSTS (HTTP 嚴格傳輸安全) 緩存使用永久存儲去加載、存儲 HSTS 策略。
該函數在 Qt 5.10 引入。
另請參閱 enableStrictTransportSecurityStore ().
Returns the current network accessibility.
該函數在 Qt 4.7 引入。
注意: getter 函數對於特性 networkAccessible .
另請參閱 setNetworkAccessible ().
[signal]
void
QNetworkAccessManager::
networkAccessibleChanged
(
QNetworkAccessManager::NetworkAccessibility
accessible
)
This signal is emitted when the value of the networkAccessible 特性改變。 accessible is the new network accessibility.
注意: 通知程序信號對於特性 networkAccessible .
把 HTTP POST (張貼) 請求發送給指定目的地通過 request 並返迴新的 QNetworkReply 為讀取而打開的對象,將包含由服務器所發送的迴復。內容對於 data 設備將上傳到服務器。
data 必須被打開以供讀取且必須保持有效,直到 finished () 信號被發射為此迴復。
注意: 發送除 HTTP HTTPS 協議的 POST (張貼) 請求是未定義的,且可能失敗。
另請參閱 get (), put (), deleteResource (),和 sendCustomRequest ().
這是重載函數。
發送內容為 data 字節數組到目的地指定通過 request .
這是重載函數。
發送內容為 multiPart 消息到目的地指定通過 request .
這可以被用於通過 HTTP 發送 MIME 多部分消息。
該函數在 Qt 4.8 引入。
另請參閱 QHttpMultiPart , QHttpPart ,和 put ().
[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 .
返迴 QNetworkProxy 請求發送使用此 QNetworkAccessManager 對象將使用。代理的默認值為 QNetworkProxy::DefaultProxy .
另請參閱 setProxy (), setProxyFactory (),和 proxyAuthenticationRequired ().
[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 ().
返迴代理工廠,此 QNetworkAccessManager 對象被用於確定要被用於請求的代理。
注意:由此函數返迴的指針的管理是通過 QNetworkAccessManager 且可以在任何時候被刪除。
該函數在 Qt 4.5 引入。
另請參閱 setProxyFactory () 和 proxy ().
上傳內容為 data 到目的地 request 並返迴新的 QNetworkReply 對象會被打開為迴復。
data 必須被打開以供讀取,當此函數被調用時;且必須保持有效直到 finished () 信號被發射為此迴復。
返迴對象是否有任何東西可供讀取,取決於協議。對於 HTTP,服務器可能發送指示上傳成功 (或不成功) 的小 HTML 頁麵。其它協議可能會在其迴復中有內容。
注意: 對於 HTTP,此請求將發送 PUT 請求 (大多數服務器不允許)。錶單上傳機製,包括透過 HTML 錶單上傳文件的機製,使用 POST 機製。
另請參閱 get (), post (), deleteResource (),和 sendCustomRequest ().
這是重載函數。
發送內容為 data 字節數組到目的地指定通過 request .
這是重載函數。
發送內容為 multiPart 消息到目的地指定通過 request .
這可以被用於通過 HTTP 發送 MIME 多部分消息。
該函數在 Qt 4.8 引入。
另請參閱 QHttpMultiPart , QHttpPart ,和 post ().
返迴所用的重定嚮策略,當創建新請求時。
該函數在 Qt 5.9 引入。
另請參閱 setRedirectPolicy () 和 QNetworkRequest::RedirectPolicy .
嚮服務器發送自定義請求標識通過 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 ().
這是重載函數。
發送內容為 data 字節數組到目的地指定通過 request .
該函數在 Qt 5.8 引入。
這是重載函數。
嚮服務器發送自定義請求標識通過 URL 的 request .
發送內容為 multiPart 消息到目的地指定通過 request .
這可用於為自定義 verb 發送 MIME 多部分消息。
該函數在 Qt 5.8 引入。
另請參閱 QHttpMultiPart , QHttpPart ,和 put ().
將管理器的網絡緩存設為 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 .
Sets the network configuration that will be used when creating the network session to config .
The network configuration is used to create and open a network session before any request that requires network access is process. If no network configuration is explicitly set via this function the network configuration returned by QNetworkConfigurationManager::defaultConfiguration () 會被使用。
To restore the default network configuration set the network configuration to the value returned from QNetworkConfigurationManager::defaultConfiguration ().
Setting a network configuration means that the QNetworkAccessManager instance will only be using the specified one. In particular, if the default network configuration changes (upon e.g. Wifi being available), this new configuration needs to be enabled manually if desired.
QNetworkConfigurationManager manager; networkAccessManager->setConfiguration(manager.defaultConfiguration());
If an invalid network configuration is set, a network session will not be created. In this case network requests will be processed regardless, but may fail. For example:
networkAccessManager->setConfiguration(QNetworkConfiguration());
該函數在 Qt 4.7 引入。
另請參閱 configuration () 和 QNetworkSession .
將管理器的 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 ().
Overrides the reported network accessibility. If accessible is NotAccessible the reported network accessiblity will always be NotAccessible . Otherwise the reported network accessibility will reflect the actual device state.
該函數在 Qt 4.7 引入。
注意: setter 函數對於特性 networkAccessible .
另請參閱 networkAccessible ().
將用於未來請求的代理設為 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 ().
將此類的代理工廠設為 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 .
例如,代理工廠可以應用以下規則:
The lifetime of the object factory will be managed by QNetworkAccessManager 。它會刪除對象,當有必要時。
注意: 若指定代理的設置是采用 setProxy (),工廠將不會被使用。
該函數在 Qt 4.5 引入。
另請參閱 proxyFactory (), setProxy (),和 QNetworkProxyQuery .
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 .
若
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 ().
[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 ().
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 .
列齣由訪問管理器支持的所有 URL 方案。
該函數在 Qt 5.2 引入。
另請參閱 supportedSchemesImplementation ().
[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 ().