内容表

上一话题

QLocalSocket

下一话题

QNetworkAddressEntry

QNetworkAccessManager

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

Inheritance diagram of PySide2.QtNetwork.QNetworkAccessManager

概要

函数

虚函数

信号

详细描述

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 基于 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 对象。返回对象被用于获取响应相应请求,而返回的任何数据。

简单下载断网,可以被贯彻采用:

manager = QNetworkAccessManager(self)
manager.finished[QNetworkReply].connect(self.replyFinished)
manager.get(QNetworkRequest(QUrl("http://qt-project.org")))
											

QNetworkAccessManager has an asynchronous API. When the replyFinished 槽被调用时,它接受的参数是 QNetworkReply 对象,包含下载数据及元数据 (Header 头、等)。

注意

在请求完成后,用户有责任删除 QNetworkReply 对象,在适当的时候。不要直接在槽内把它删除,槽被连接到 finished() . You can use the 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.

假定管理器已经存在,更复杂范例可以是:

request = QNetworkRequest()
request.setUrl(QUrl("http://qt-project.org"))
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0")
reply = manager.get(request)
reply.readyRead.connect(self.slotReadyRead)
reply.error[QNetworkReply.NetworkError].connect(self..slotError)
reply.sslErrors.connect(self.slotSslErrors)
											
class QNetworkAccessManager ( [ parent=None ] )
param parent

QObject

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

PySide2.QtNetwork.QNetworkAccessManager. 操作

指示此回复正在处理的操作。

常量

描述

QNetworkAccessManager.HeadOperation

检索 Header 头操作 (创建采用 head() )

QNetworkAccessManager.GetOperation

检索 Header 头并下载内容 (创建采用 get() )

QNetworkAccessManager.PutOperation

上传内容操作 (创建采用 put() )

QNetworkAccessManager.PostOperation

经由 HTTP POST 发送用于处理的 HTML 表单内容 (创建采用 post() )

QNetworkAccessManager.DeleteOperation

删除内容操作 (创建采用 deleteResource() )

QNetworkAccessManager.CustomOperation

自定义操作 (创建采用 sendCustomRequest() )

另请参阅

operation()

PySide2.QtNetwork.QNetworkAccessManager. NetworkAccessibility

Indicates whether the network is accessible via this network access manager.

常量

描述

QNetworkAccessManager.UnknownAccessibility

网络的可访问性不能确定。

QNetworkAccessManager.NotAccessible

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

网络是可访问的。

另请参阅

networkAccessible

New in version 4.7.

PySide2.QtNetwork.QNetworkAccessManager. activeConfiguration ( )
返回类型

QNetworkConfiguration

注意

此函数被弃用。

Returns the current active network configuration.

If the network configuration returned by configuration() 是类型 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.

另请参阅

configuration()

PySide2.QtNetwork.QNetworkAccessManager. addStrictTransportSecurityHosts ( knownHosts )
参数

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.

PySide2.QtNetwork.QNetworkAccessManager. authenticationRequired ( reply , authenticator )
参数
PySide2.QtNetwork.QNetworkAccessManager. autoDeleteReplies ( )
返回类型

bool

返回 true 若 QNetworkAccessManager 目前被配置为自动删除 QNetworkReplies,否则 false。

另请参阅

setAutoDeleteReplies AutoDeleteReplyOnFinishAttribute

PySide2.QtNetwork.QNetworkAccessManager. cache ( )
返回类型

QAbstractNetworkCache

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

另请参阅

setCache()

PySide2.QtNetwork.QNetworkAccessManager. clearAccessCache ( )

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

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

PySide2.QtNetwork.QNetworkAccessManager. clearConnectionCache ( )

刷新网络连接的内部缓存。相比 clearAccessCache() the authentication data is preserved.

PySide2.QtNetwork.QNetworkAccessManager. configuration ( )
返回类型

QNetworkConfiguration

注意

此函数被弃用。

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

PySide2.QtNetwork.QNetworkAccessManager. connectToHost ( hostName [ , port=80 ] )
参数
  • hostName – unicode

  • port quint16

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

注意

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

PySide2.QtNetwork.QNetworkAccessManager. connectToHostEncrypted ( hostName [ , port=443 [ , 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.

注意

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

PySide2.QtNetwork.QNetworkAccessManager. connectToHostEncrypted ( hostName , port , sslConfiguration , peerName )
参数
  • hostName – unicode

  • port quint16

  • sslConfiguration QSslConfiguration

  • peerName – unicode

这是重载函数。

初启连接到主机,给定通过 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.

注意

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

PySide2.QtNetwork.QNetworkAccessManager. cookieJar ( )
返回类型

QNetworkCookieJar

返回 QNetworkCookieJar 用于存储从网络获得的 Cookie 及即将被发送的 Cookie。

另请参阅

setCookieJar()

PySide2.QtNetwork.QNetworkAccessManager. createRequest ( op , request [ , outgoingData=None ] )
参数
返回类型

QNetworkReply

返回新 QNetworkReply 对象以处理操作 op 和请求 originalReq 。设备 outgoingData 始终为 0 对于 Get 和 Head 请求而言,但是值被传递给 post() and put() in those operations (the QByteArray 变体会传递 QBuffer 对象)。

默认实现调用 cookiesForUrl() on the cookie jar set with setCookieJar() to obtain the cookies to be sent to the remote server.

返回的对象必须处于打开状态。

PySide2.QtNetwork.QNetworkAccessManager. deleteResource ( request )
参数

request QNetworkRequest

返回类型

QNetworkReply

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

注意

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

PySide2.QtNetwork.QNetworkAccessManager. enableStrictTransportSecurityStore ( enabled [ , storeDir="" ] )
参数
  • enabled bool

  • storeDir – unicode

enabled is true ,内部 HSTS (HTTP 严格传输安全) 缓存将使用持久存储来读取和写入 HSTS 策略。 storeDir defines where this store will be located. The default location is defined by 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.

PySide2.QtNetwork.QNetworkAccessManager. encrypted ( reply )
参数

reply QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. finished ( reply )
参数

reply QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. get ( request )
参数

request QNetworkRequest

返回类型

QNetworkReply

张贴请求以获取内容对于目标 request 并返回新 QNetworkReply 打开对象为读取其发射的 readyRead() 信号,每当新数据到达时。

内容及关联 Header 头会被下载。

PySide2.QtNetwork.QNetworkAccessManager. head ( request )
参数

request QNetworkRequest

返回类型

QNetworkReply

张贴请求以获取网络头为 request 并返回新 QNetworkReply 对象 (包含这样的 Header 头)。

函数以 HTTP 请求关联 HEAD 头命名。

PySide2.QtNetwork.QNetworkAccessManager. isStrictTransportSecurityEnabled ( )
返回类型

bool

返回 true,若 HSTS (HTTP 严格传输安全) 被启用。默认情况下,HSTS 是禁用的。

PySide2.QtNetwork.QNetworkAccessManager. isStrictTransportSecurityStoreEnabled ( )
返回类型

bool

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

PySide2.QtNetwork.QNetworkAccessManager. networkAccessible ( )
返回类型

NetworkAccessibility

注意

此函数被弃用。

Returns the current network accessibility.

PySide2.QtNetwork.QNetworkAccessManager. networkAccessibleChanged ( accessible )
参数

accessible NetworkAccessibility

注意

此函数被弃用。

PySide2.QtNetwork.QNetworkAccessManager. networkSessionConnected ( )

注意

此函数被弃用。

PySide2.QtNetwork.QNetworkAccessManager. post ( request , multiPart )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. post ( request , data )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. post ( request , data )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. preSharedKeyAuthenticationRequired ( reply , authenticator )
参数
PySide2.QtNetwork.QNetworkAccessManager. proxy ( )
返回类型

QNetworkProxy

返回 QNetworkProxy 请求发送使用此 QNetworkAccessManager 对象将使用。代理的默认值为 DefaultProxy .

PySide2.QtNetwork.QNetworkAccessManager. proxyAuthenticationRequired ( proxy , authenticator )
参数
PySide2.QtNetwork.QNetworkAccessManager. proxyFactory ( )
返回类型

QNetworkProxyFactory

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

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

PySide2.QtNetwork.QNetworkAccessManager. put ( request , multiPart )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. put ( request , data )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. put ( request , data )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. redirectPolicy ( )
返回类型

RedirectPolicy

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

另请参阅

setRedirectPolicy() RedirectPolicy

PySide2.QtNetwork.QNetworkAccessManager. sendCustomRequest ( request , verb , data )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. sendCustomRequest ( request , verb , multiPart )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. sendCustomRequest ( request , verb [ , data=None ] )
参数
返回类型

QNetworkReply

PySide2.QtNetwork.QNetworkAccessManager. setAutoDeleteReplies ( autoDelete )
参数

autoDelete bool

启用 (或禁用) 自动删除 QNetworkReplies .

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

另请参阅

autoDeleteReplies AutoDeleteReplyOnFinishAttribute

PySide2.QtNetwork.QNetworkAccessManager. setCache ( cache )
参数

cache QAbstractNetworkCache

Sets the manager’s network cache to be the 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.

另请参阅

cache() CacheLoadControl

PySide2.QtNetwork.QNetworkAccessManager. setConfiguration ( config )
参数

config QNetworkConfiguration

注意

此函数被弃用。

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 defaultConfiguration() 会被使用。

To restore the default network configuration set the network configuration to the value returned from 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.

manager = QNetworkConfigurationManager()
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())
											
PySide2.QtNetwork.QNetworkAccessManager. setCookieJar ( cookieJar )
参数

cookieJar QNetworkCookieJar

Sets the manager’s cookie jar to be the 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 cookiesForUrl() and setCookiesFromUrl() virtual functions. Those functions are called by QNetworkAccessManager when it detects a new cookie.

PySide2.QtNetwork.QNetworkAccessManager. setNetworkAccessible ( accessible )
参数

accessible NetworkAccessibility

注意

此函数被弃用。

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.

PySide2.QtNetwork.QNetworkAccessManager. setProxy ( proxy )
参数

proxy QNetworkProxy

将用于未来请求的代理设为 proxy 。这不会影响已经发送的请求。 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() .

PySide2.QtNetwork.QNetworkAccessManager. setProxyFactory ( factory )
参数

factory QNetworkProxyFactory

将此类的代理工厂设为 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 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 NoProxy

  • 若请求为 FTP,返回 FTP 代理

  • 若请求为 HTTP 或 HTTPS,返回 HTTP 代理

  • 否则,返回 SOCKSv5 代理服务器

The lifetime of the object factory will be managed by QNetworkAccessManager 。它会删除对象当有必要时。

注意

如果指定代理被设置采用 setProxy() , the factory will not be used.

PySide2.QtNetwork.QNetworkAccessManager. setRedirectPolicy ( policy )
参数

policy RedirectPolicy

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

Use this function to enable or disable HTTP redirects on the manager’s level.

注意

当创建请求时,QNetworkRequest::RedirectAttributePolicy 拥有最高优先级,下一优先级是 FollowRedirectsAttribute . Finally, the manager’s policy has the lowest priority.

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

另请参阅

redirectPolicy() RedirectPolicy FollowRedirectsAttribute

PySide2.QtNetwork.QNetworkAccessManager. setStrictTransportSecurityEnabled ( enabled )
参数

enabled bool

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.

PySide2.QtNetwork.QNetworkAccessManager. setTransferTimeout ( [ timeout=QNetworkRequest.DefaultTransferTimeoutConstant ] )
参数

timeout int

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 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.

另请参阅

transferTimeout()

PySide2.QtNetwork.QNetworkAccessManager. sslErrors ( reply , errors )
参数
PySide2.QtNetwork.QNetworkAccessManager. strictTransportSecurityHosts ( )
返回类型

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.

PySide2.QtNetwork.QNetworkAccessManager. supportedSchemes ( )
返回类型

字符串列表

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

PySide2.QtNetwork.QNetworkAccessManager. supportedSchemesImplementation ( )
返回类型

字符串列表

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

您不应该直接调用此函数。使用 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.

PySide2.QtNetwork.QNetworkAccessManager. transferTimeout ( )
返回类型

int

返回用于传输的超时 (以毫秒为单位)。

此超时为 0 若 setTransferTimeout() hasn’t been called, which means that the timeout is not used.