QNetworkAccessManager 类

QNetworkAccessManager class allows the application to send network requests and receive replies. 更多...

Header: #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)

保护槽

额外继承成员

详细描述

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 .

成员类型文档编制

enum QNetworkAccessManager:: NetworkAccessibility

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 .

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

This enum was introduced or modified in Qt 4.7.

另请参阅 QNetworkReply::operation ().

特性文档编制

networkAccessible : NetworkAccessibility

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.

This property was introduced in Qt 4.7.

访问函数:

QNetworkAccessManager::NetworkAccessibility networkAccessible () const
void setNetworkAccessible (QNetworkAccessManager::NetworkAccessibility accessible )

通知信号:

void networkAccessibleChanged (QNetworkAccessManager::NetworkAccessibility accessible )

成员函数文档编制

QNetworkAccessManager:: QNetworkAccessManager ( QObject * parent = nullptr)

构造 QNetworkAccessManager object that is the center of the Network Access API and sets parent 作为父级对象。

[virtual] QNetworkAccessManager:: ~QNetworkAccessManager ()

销毁 QNetworkAccessManager object and frees up any resources. Note that 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.

QNetworkConfiguration QNetworkAccessManager:: activeConfiguration () const

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.

This function was introduced in Qt 4.7.

另请参阅 configuration ().

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

Adds HTTP Strict Transport Security policies into HSTS cache. knownHosts contains the known hosts that have QHstsPolicy information.

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

注意: While processing HTTP responses, 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.

This function was introduced in Qt 5.9.

另请参阅 addStrictTransportSecurityHosts(), enableStrictTransportSecurityStore (),和 QHstsPolicy .

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

This signal is emitted whenever a final server requests authentication before it delivers the requested contents. The slot connected to this signal should fill the credentials for the contents (which can be determined by inspecting the reply 对象) 在 authenticator 对象。

QNetworkAccessManager 会在内部缓存证书并发送相同值,若服务器再次要求身份验证,而不发射 authenticationRequired() 信号。若它拒绝证书,此信号会被再次发射。

注意: 要让请求不发送证书,必须不调用 setUser() 或 setPassword() 在 authenticator 对象。这将导致 finished () 信号被发射采用 QNetworkReply 采用错误 AuthenticationRequiredError .

注意: 使用 QueuedConnection 去连接到此信号是不可能的,因为连接会失败,若身份验证器没有采新信息被填充,当信号返回时。

另请参阅 proxyAuthenticationRequired (), QAuthenticator::setUser (),和 QAuthenticator::setPassword ().

QAbstractNetworkCache *QNetworkAccessManager:: cache () const

返回用于存储从网络获得数据的缓存。

This function was introduced in Qt 4.5.

另请参阅 setCache ().

void QNetworkAccessManager:: clearAccessCache ()

刷新身份验证数据和网络连接的内部缓存。

此函数对履行自动测试很有用。

This function was introduced in Qt 5.0.

另请参阅 clearConnectionCache ().

void QNetworkAccessManager:: clearConnectionCache ()

刷新网络连接的内部缓存。相比 clearAccessCache () 预留身份验证数据。

This function was introduced in Qt 5.9.

另请参阅 clearAccessCache ().

QNetworkConfiguration QNetworkAccessManager:: configuration () const

Returns the network configuration that will be used to create the network session which will be used when processing network requests.

This function was introduced in Qt 4.7.

另请参阅 setConfiguration () 和 activeConfiguration ().

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

初启连接到主机,给定通过 hostName 在端口 port 。此函数对在发出 HTTP 请求之前完成与主机的 TCP 握手很有用,从而降低网络延迟。

注意: 此函数没有可能去报告错误。

This function was introduced in 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.

注意: 此函数没有可能去报告错误。

This function was introduced in Qt 5.2.

另请参阅 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 )

发送请求以删除资源,标识通过 request URL.

注意: 此特征目前只可用于 HTTP,履行 HTTP DELETE 请求。

This function was introduced in Qt 4.6.

另请参阅 get (), post (), put (),和 sendCustomRequest ().

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

enabled is true , the internal HSTS cache will use a persistent store to read and write HSTS policies. 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.

This function was introduced in Qt 5.10.

另请参阅 isStrictTransportSecurityStoreEnabled (), setStrictTransportSecurityEnabled (),和 QStandardPaths::standardLocations ().

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

This signal is emitted when an SSL/TLS session has successfully completed the initial handshake. At this point, no user data has been transmitted. The signal can be used to perform additional checks on the certificate chain, for example to notify users when the certificate for a website has changed. The reply parameter specifies which network reply is responsible. If the reply does not match the expected criteria then it should be aborted by calling QNetworkReply::abort () by a slot connected to this signal. The SSL configuration in use can be inspected using the QNetworkReply::sslConfiguration () 方法。

内部, QNetworkAccessManager may open multiple connections to a server, in order to allow it process requests in parallel. These connections may be reused, which means that the encrypted() signal would not be emitted. This means that you are only guaranteed to receive this signal for the first connection to a site in the lifespan of the QNetworkAccessManager .

This function was introduced in Qt 5.1.

另请参阅 QSslSocket::encrypted () 和 QNetworkReply::encrypted ().

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

此信号被发射每当待决网络回复完成时。 reply 参数将包含指向刚刚完成的回复的指针。此信号串联发射 QNetworkReply::finished () 信号。

QNetworkReply::finished () 了解对象将处于何种状态的有关信息。

注意: 不要删除 reply 对象在连接到此信号的槽中。使用 deleteLater ().

另请参阅 QNetworkReply::finished () 和 QNetworkReply::error ().

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 是禁用的。

This function was introduced in Qt 5.9.

另请参阅 setStrictTransportSecurityEnabled ().

bool QNetworkAccessManager:: isStrictTransportSecurityStoreEnabled () const

返回 true,若 HSTS (HTTP 严格传输安全) 缓存使用永久存储去加载、存储 HSTS 策略。

This function was introduced in Qt 5.10.

另请参阅 enableStrictTransportSecurityStore ().

QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager:: networkAccessible () const

Returns the current network accessibility.

This function was introduced in Qt 4.7.

注意: Getter 函数为特性 networkAccessible .

另请参阅 setNetworkAccessible ().

[signal] void QNetworkAccessManager:: networkAccessibleChanged ( QNetworkAccessManager::NetworkAccessibility accessible )

This signal is emitted when the value of the networkAccessible property changes. accessible is the new network accessibility.

注意: 通知信号为特性 networkAccessible .

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 多部分消息。

This function was introduced in 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 object is the QNetworkReply that is negotiating such ciphersuites.

当使用 PSK 时,客户端必须向服务器发送有效标识和有效 PSK (预共享密钥) 以便 SSL 握手得以继续。应用程序可以在此信号连接的槽中提供此信息,通过填入传递的 authenticator 对象根据需要。

注意: 忽略此信号或未能提供要求证书,将导致握手失败,因此连接将被中止。

注意: authenticator object is owned by the reply and must not be deleted by the application.

This function was introduced in Qt 5.5.

另请参阅 QSslPreSharedKeyAuthenticator .

QNetworkProxy QNetworkAccessManager:: proxy () const

返回 QNetworkProxy 其将被使用,以使用此 QNetworkAccessManager 对象去发送请求。代理的默认值是 QNetworkProxy::DefaultProxy .

另请参阅 setProxy (), setProxyFactory (),和 proxyAuthenticationRequired ().

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

This signal is emitted whenever a proxy requests authentication and 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.

If the proxy rejects the credentials, QNetworkAccessManager will emit the signal again.

另请参阅 proxy (), setProxy (),和 authenticationRequired ().

QNetworkProxyFactory *QNetworkAccessManager:: proxyFactory () const

返回代理工厂,此 QNetworkAccessManager 对象被用于确定要被用于请求的代理。

注意:由此函数返回的指针的管理是通过 QNetworkAccessManager 且可以随时被删除。

This function was introduced in 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 多部分消息。

This function was introduced in Qt 4.8.

另请参阅 QHttpMultiPart , QHttpPart ,和 post ().

QNetworkRequest::RedirectPolicy QNetworkAccessManager:: redirectPolicy () const

返回所用的重定向策略,当创建新请求时。

This function was introduced in Qt 5.9.

另请参阅 setRedirectPolicy () 和 QNetworkRequest::RedirectPolicy .

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

Sends a custom request to the server identified by the URL of request .

It is the user's responsibility to send a verb to the server that is valid according to the HTTP specification.

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 () 信号被发射为此回复。

注意: This feature is currently available for HTTP(S) only.

This function was introduced in Qt 4.7.

另请参阅 get (), post (), put (),和 deleteResource ().

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

这是重载函数。

发送 data 字节数组内容到指定目的地通过 request .

This function was introduced in Qt 5.8.

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

这是重载函数。

Sends a custom request to the server identified by the URL of request .

发送 multiPart 消息内容到指定目的地通过 request .

This can be used for sending MIME multipart messages for custom verbs.

This function was introduced in Qt 5.8.

另请参阅 QHttpMultiPart , QHttpPart ,和 put ().

void QNetworkAccessManager:: setCache ( QAbstractNetworkCache * cache )

Sets the manager's network cache to be the cache specified. The cache is used for all requests dispatched by the manager.

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

注意: QNetworkAccessManager takes ownership of the cache 对象。

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

This function was introduced in Qt 4.5.

另请参阅 cache () 和 QNetworkRequest::CacheLoadControl .

void QNetworkAccessManager:: setConfiguration (const QNetworkConfiguration & config )

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 () will be used.

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

This function was introduced in Qt 4.7.

另请参阅 configuration () 和 QNetworkSession .

void QNetworkAccessManager:: setCookieJar ( QNetworkCookieJar * cookieJar )

Sets the manager's cookie jar to be the cookieJar specified. The cookie jar is used by all requests dispatched by the manager.

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

注意: QNetworkAccessManager takes ownership of the 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:: setNetworkAccessible ( QNetworkAccessManager::NetworkAccessibility accessible )

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.

This function was introduced in Qt 4.7.

注意: Setter 函数为特性 networkAccessible .

另请参阅 networkAccessible ().

void QNetworkAccessManager:: setProxy (const QNetworkProxy & proxy )

Sets the proxy to be used in future requests to be proxy . This does not affect requests that have already been sent. The proxyAuthenticationRequired () signal will be emitted if the proxy requests authentication.

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 )

Sets the proxy factory for this class to be 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 .

For example, a proxy factory could apply the following rules:

  • 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
  • if the request is FTP, return an FTP proxy
  • if the request is HTTP or HTTPS, then return an HTTP proxy
  • otherwise, return a SOCKSv5 proxy server

The lifetime of the object factory will be managed by QNetworkAccessManager . It will delete the object when necessary.

注意: 如果指定代理被设置采用 setProxy (),工厂将不会被使用。

This function was introduced in Qt 4.5.

另请参阅 proxyFactory (), setProxy (),和 QNetworkProxyQuery .

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

把管理器的重定向策略设为指定 policy 。此策略将影响由管理器创建的所有后续请求。

使用此函数在管理器级别启用或禁用 HTTP 重定向。

注意: 当创建请求时,QNetworkRequest::RedirectAttributePolicy 拥有最高优先级,下一优先级是 QNetworkRequest::FollowRedirectsAttribute 。最后,管理器的策略拥有最低优先级。

为向后兼容,默认值为 QNetworkRequest::ManualRedirectPolicy 。未来可能改变,且某些类型的自动重定向策略将变为默认;鼓励依赖手动处理重定向的客户端,在其代码中明确设置此策略。

This function was introduced in 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.

This function was introduced in Qt 5.9.

另请参阅 isStrictTransportSecurityEnabled ().

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

This signal is emitted if the SSL/TLS session encountered errors during the set up, including certificate verification errors. The errors parameter contains the list of errors and reply is the QNetworkReply that is encountering these errors.

To indicate that the errors are not fatal and that the connection should proceed, the QNetworkReply::ignoreSslErrors () function should be called from the slot connected to this signal. If it is not called, the SSL session will be torn down before any data is exchanged (including the 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 ().

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.

This function was introduced in Qt 5.9.

另请参阅 addStrictTransportSecurityHosts () 和 QHstsPolicy .

QStringList QNetworkAccessManager:: supportedSchemes () const

列出由访问管理器支持的所有 URL 方案。

This function was introduced in Qt 5.2.

另请参阅 supportedSchemesImplementation ().

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

列出由访问管理器支持的所有 URL 方案。

You should not call this function directly; use 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.

This function was introduced in Qt 5.2.

另请参阅 supportedSchemes ().