QNetworkAccessManager

概要

函数

虚函数

信号

详细描述

PySide.QtNetwork.QNetworkAccessManager class allows the application to send network requests and receive replies

The Network Access API is constructed around one PySide.QtNetwork.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 PySide.QtNetwork.QNetworkAccessManager should be enough for the whole Qt application.

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

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

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

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

注意

在请求完成后,用户有责任删除 PySide.QtNetwork.QNetworkReply 对象,在适当的时候。不要直接在槽内把它删除,槽被连接到 PySide.QtNetwork.QNetworkAccessManager.finished() . You can use the PySide.QtCore.QObject.deleteLater() 函数。

注意

PySide.QtNetwork.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.nokia.com"))
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)
									

Network and Roaming support

With the addition of the 承载管理 API to Qt 4.7 PySide.QtNetwork.QNetworkAccessManager gained the ability to manage network connections. PySide.QtNetwork.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 platform 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 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 PySide.QtNetwork.QNetworkAccessManager is conditional upon the platform supporting connection management. The QNetworkConfigurationManager.NetworkSessionRequired can be used to detect whether PySide.QtNetwork.QNetworkAccessManager utilizes this feature. Currently only Meego/Harmattan and Symbian platforms provide connection management support.

注意

This feature cannot be used in combination with the Bearer Management API as provided by QtMobility. Applications have to migrate to the Qt version of Bearer Management.

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, operations will result in a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

class PySide.QtNetwork. QNetworkAccessManager ( [ parent=None ] )
参数: parent PySide.QtCore.QObject

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

PySide.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 PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() .
QNetworkAccessManager.Accessible 网络是可访问的。

注意

This enum was introduced or modified in Qt 4.7

PySide.QtNetwork.QNetworkAccessManager. 操作

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

常量 描述
QNetworkAccessManager.HeadOperation 检索 Header 头操作 (创建采用 PySide.QtNetwork.QNetworkAccessManager.head() )
QNetworkAccessManager.GetOperation 检索 Header 头并下载内容 (创建采用 PySide.QtNetwork.QNetworkAccessManager.get() )
QNetworkAccessManager.PutOperation 上传内容操作 (创建采用 PySide.QtNetwork.QNetworkAccessManager.put() )
QNetworkAccessManager.PostOperation 经由 HTTP POST 发送用于处理的 HTML 表单内容 (创建采用 PySide.QtNetwork.QNetworkAccessManager.post() )
QNetworkAccessManager.DeleteOperation 删除内容操作 (创建采用 PySide.QtNetwork.QNetworkAccessManager.deleteResource() )
QNetworkAccessManager.CustomOperation 自定义操作 (创建采用 PySide.QtNetwork.QNetworkAccessManager.sendCustomRequest() )
PySide.QtNetwork.QNetworkAccessManager. activeConfiguration ( )
返回类型: PySide.QtNetwork.QNetworkConfiguration

Returns the current active network configuration.

If the network configuration returned by PySide.QtNetwork.QNetworkAccessManager.configuration() 是类型 QNetworkConfiguration.ServiceNetwork this function will return the current active child network configuration of that configuration. Otherwise returns the same network configuration as PySide.QtNetwork.QNetworkAccessManager.configuration() .

Use this function to return the actual network configuration currently in use by the network session.

PySide.QtNetwork.QNetworkAccessManager. authenticationRequired ( reply , authenticator )
参数:
PySide.QtNetwork.QNetworkAccessManager. cache ( )
返回类型: PySide.QtNetwork.QAbstractNetworkCache

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

PySide.QtNetwork.QNetworkAccessManager. configuration ( )
返回类型: PySide.QtNetwork.QNetworkConfiguration

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

PySide.QtNetwork.QNetworkAccessManager. cookieJar ( )
返回类型: PySide.QtNetwork.QNetworkCookieJar

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

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

PySide.QtNetwork.QNetworkReply

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

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

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

PySide.QtNetwork.QNetworkAccessManager. deleteResource ( request )
参数: request PySide.QtNetwork.QNetworkRequest
返回类型: PySide.QtNetwork.QNetworkReply

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

注意

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

PySide.QtNetwork.QNetworkAccessManager. finished ( reply )
参数: reply PySide.QtNetwork.QNetworkReply
PySide.QtNetwork.QNetworkAccessManager. get ( request )
参数: request PySide.QtNetwork.QNetworkRequest
返回类型: PySide.QtNetwork.QNetworkReply

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

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

PySide.QtNetwork.QNetworkAccessManager. head ( request )
参数: request PySide.QtNetwork.QNetworkRequest
返回类型: PySide.QtNetwork.QNetworkReply

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

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

PySide.QtNetwork.QNetworkAccessManager. networkAccessible ( )
返回类型: PySide.QtNetwork.QNetworkAccessManager.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 reenabled again by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.Accessible)
												

注意

调用 PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() does not change the network state.

PySide.QtNetwork.QNetworkAccessManager. networkAccessibleChanged ( accessible )
参数: accessible PySide.QtNetwork.QNetworkAccessManager.NetworkAccessibility
PySide.QtNetwork.QNetworkAccessManager. networkSessionConnected ( )
PySide.QtNetwork.QNetworkAccessManager. post ( request , data )
参数:
返回类型:

PySide.QtNetwork.QNetworkReply

这是重载函数。

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

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

PySide.QtNetwork.QNetworkReply

把 HTTP POST (张贴) 请求发送给指定目的地通过 request 并返回新 PySide.QtNetwork.QNetworkReply 为读取而打开的对象,将包含由服务器所发送的回复。内容对于 data 设备将上传到服务器。

data 必须被打开以供读取且必须保持有效,直到 PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

注意

发送除 HTTP HTTPS 协议的 POST (张贴) 请求是未定义的,且可能失败。

PySide.QtNetwork.QNetworkAccessManager. proxy ( )
返回类型: PySide.QtNetwork.QNetworkProxy

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

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

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

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

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

PySide.QtNetwork.QNetworkReply

上传内容为 data 到目的地 request and returnes a new PySide.QtNetwork.QNetworkReply 打开对象为回复。

data 必须被打开以供读取,当此函数被调用时;且必须保持有效直到 PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

返回对象是否有任何东西可供读取,取决于协议。对于 HTTP,服务器可能发送指示上传成功 (或不成功) 的小 HTML 页面。其它协议可能会在其回复中有内容。

注意

对于 HTTP,此请求将发送 PUT 请求 (大多数服务器不允许)。表单上传机制,包括透过 HTML 表单上传文件的机制,使用 POST 机制。

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

PySide.QtNetwork.QNetworkReply

这是重载函数。

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

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

PySide.QtNetwork.QNetworkReply

向服务器发送自定义请求标识通过 URL 的 request .

用户负责发送 verb 给根据 HTTP 规范有效的服务器。

This method provides means to send verbs other than the common ones provided via PySide.QtNetwork.QNetworkAccessManager.get() or PySide.QtNetwork.QNetworkAccessManager.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 PySide.QtNetwork.QNetworkAccessManager.finished() signal is emitted for this reply.

注意

此特征目前只可用于 HTTP(S)。

PySide.QtNetwork.QNetworkAccessManager. setCache ( cache )
参数: cache PySide.QtNetwork.QAbstractNetworkCache

将管理器的网络缓存设为 cache 指定。缓存用于由管理器分派的所有请求。

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

注意

PySide.QtNetwork.QNetworkAccessManager 拥有所有权对于 cache 对象。

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

另请参阅

PySide.QtNetwork.QNetworkAccessManager.cache() QNetworkRequest.CacheLoadControl

PySide.QtNetwork.QNetworkAccessManager. setConfiguration ( config )
参数: config PySide.QtNetwork.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 QNetworkConfigurationManager.defaultConfiguration() 会被使用。

To restore the default network configuration set the network configuration to the value returned from QNetworkConfigurationManager.defaultConfiguration() .

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())
												
PySide.QtNetwork.QNetworkAccessManager. setCookieJar ( cookieJar )
参数: cookieJar PySide.QtNetwork.QNetworkCookieJar

将管理器的 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.

注意

PySide.QtNetwork.QNetworkAccessManager 拥有所有权对于 cookieJar 对象。

cookieJar is in the same thread as this PySide.QtNetwork.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 PySide.QtNetwork.QNetworkAccessManager objects, you may want to set the cookie jar's parent to 0 after calling this function.

PySide.QtNetwork.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() and QNetworkCookieJar.setCookiesFromUrl() virtual functions. Those functions are called by PySide.QtNetwork.QNetworkAccessManager when it detects a new cookie.

PySide.QtNetwork.QNetworkAccessManager. setNetworkAccessible ( accessible )
参数: accessible PySide.QtNetwork.QNetworkAccessManager.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 reenabled again by calling

networkAccessManager.setNetworkAccessible(QNetworkAccessManager.Accessible)
												

注意

调用 PySide.QtNetwork.QNetworkAccessManager.setNetworkAccessible() does not change the network state.

PySide.QtNetwork.QNetworkAccessManager. setProxy ( proxy )
参数: proxy PySide.QtNetwork.QNetworkProxy

将用于未来请求的代理设为 proxy 。这不会影响已经发送的请求。 PySide.QtNetwork.QNetworkAccessManager.proxyAuthenticationRequired() signal will be emitted if the proxy requests authentication.

A proxy set with this function will be used for all requests issued by PySide.QtNetwork.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 PySide.QtNetwork.QNetworkAccessManager.setProxyFactory() .

PySide.QtNetwork.QNetworkAccessManager. setProxyFactory ( factory )
参数: factory PySide.QtNetwork.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 PySide.QtNetwork.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 PySide.QtNetwork.QNetworkAccessManager 。它会删除对象当有必要时。

注意

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

PySide.QtNetwork.QNetworkAccessManager. sslErrors ( reply , errors )
参数: