QNetworkAccessManager Class Reference

[ QtNetwork module]

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

继承 QObject .

类型

方法

Qt Signals


详细描述

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 should be enough for the whole Qt application.

Once a 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 object. The returned object is used to obtain any data returned in response to the corresponding request.

A simple download off the network could be accomplished with:

 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
 connect(manager, SIGNAL(finished(QNetworkReply*)),
         this, SLOT(replyFinished(QNetworkReply*)));
 manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
			

QNetworkAccessManager 拥有异步 API。当 replyFinished slot above is called, the parameter it takes 是 QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.).

注意: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished (). You 可以使用 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.

A more involved example, assuming the manager is already existent, can be:

 QNetworkRequest request;
 request.setUrl(QUrl("http://qt.nokia.com"));
 request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
 QNetworkReply *reply = manager->get(request);
 connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
 connect(reply, SIGNAL(error(QNetworkReply.NetworkError)),
         this, SLOT(slotError(QNetworkReply.NetworkError)));
 connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
         this, SLOT(slotSslErrors(QList<QSslError>)));
			

Network and Roaming support

With the addition of the Bearer Management 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 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 QNetworkAccessManager is conditional upon the platform supporting connection management. The QNetworkConfigurationManager.NetworkSessionRequired can be used to detect whether 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.


类型文档编制

QNetworkAccessManager.NetworkAccessibility

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

常量 描述
QNetworkAccessManager.UnknownAccessibility -1 The network accessibility cannot be determined.
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.Operation

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

常量 描述
QNetworkAccessManager.HeadOperation 1 检索 Header 头操作 (创建采用 head ())
QNetworkAccessManager.GetOperation 2 retrieve headers and download contents (created with get ())
QNetworkAccessManager.PutOperation 3 上传内容操作 (创建采用 put ())
QNetworkAccessManager.PostOperation 4 send the contents of an HTML form for processing via HTTP POST (created with post ())
QNetworkAccessManager.DeleteOperation 5 删除内容操作 (创建采用 deleteResource ())
QNetworkAccessManager.CustomOperation 6 自定义操作 (创建采用 sendCustomRequest ())

该枚举在 Qt 4.7 引入或被修改。

另请参阅 QNetworkReply.operation ().


方法文档编制

QNetworkAccessManager.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QNetworkAccessManager object that is the center of the Network Access API and sets parent as the parent object.

QNetworkConfiguration QNetworkAccessManager.activeConfiguration ( self )

Returns the current active network configuration.

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

QAbstractNetworkCache QNetworkAccessManager.cache ( self )

Returns the cache that is used to store data obtained from the network.

该函数在 Qt 4.5 引入。

另请参阅 setCache ().

QNetworkConfiguration QNetworkAccessManager.configuration ( self )

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 () and activeConfiguration ().

QNetworkCookieJar QNetworkAccessManager.cookieJar ( self )

返回 QNetworkCookieJar that is used to store cookies obtained from the network as well as cookies that are about to be sent.

另请参阅 setCookieJar ().

QNetworkReply QNetworkAccessManager.createRequest ( self , 操作   op , QNetworkRequest   request , QIODevice   device  = None)

返回新 QNetworkReply 对象以处理操作 op 和请求 req . The device outgoingData is always 0 for Get and Head requests, but is the value passed to post () 和 put () 在这些操作中 ( QByteArray 变体会传递 QBuffer 对象)。

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

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

QNetworkReply QNetworkAccessManager.deleteResource ( self , QNetworkRequest   request )

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

注意: This feature is currently available for HTTP only, performing an HTTP DELETE request.

该函数在 Qt 4.6 引入。

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

QNetworkReply QNetworkAccessManager.get ( self , QNetworkRequest   request )

张贴请求以获取内容对于目标 request 并返回新 QNetworkReply object opened for reading which emits the readyRead() 信号,每当新数据到达时。

The contents as well as associated headers will be downloaded.

另请参阅 post (), put (), deleteResource (), and sendCustomRequest ().

QNetworkReply QNetworkAccessManager.head ( self , QNetworkRequest   request )

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

The function is named after the HTTP request associated (HEAD).

NetworkAccessibility QNetworkAccessManager.networkAccessible ( self )

QNetworkReply QNetworkAccessManager.post ( self , QNetworkRequest   request , QIODevice   data )

把 HTTP POST (张贴) 请求发送给指定目的地通过 request 并返回新 QNetworkReply object opened for reading that will contain the reply sent by the server. The contents of the data 设备将上传到服务器。

data must be open for reading and must remain valid until the finished () signal is emitted for this reply.

注意: Sending a POST request on protocols other than HTTP and HTTPS is undefined and will probably fail.

另请参阅 get (), put (), deleteResource (), and sendCustomRequest ().

QNetworkReply QNetworkAccessManager.post ( self , QNetworkRequest   request , QByteArray   data )

这是重载函数。

发送内容为 data byte array to the destination specified by request .

QNetworkReply QNetworkAccessManager.post ( self , QNetworkRequest   request , QHttpMultiPart   multiPart )

这是重载函数。

发送内容为 multiPart message to the destination specified by request .

This can be used for sending MIME multipart messages over HTTP.

该函数在 Qt 4.8 引入。

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

QNetworkProxy QNetworkAccessManager.proxy ( self )

返回 QNetworkProxy that the requests sent using this QNetworkAccessManager object will use. The default value for the proxy is QNetworkProxy.DefaultProxy .

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

QNetworkProxyFactory QNetworkAccessManager.proxyFactory ( self )

返回代理工厂,此 QNetworkAccessManager object is using to determine the proxies to be used for requests.

注意:由此函数返回的指针的管理是通过 QNetworkAccessManager and could be deleted at any time.

该函数在 Qt 4.5 引入。

另请参阅 setProxyFactory () and proxy ().

QNetworkReply QNetworkAccessManager.put ( self , QNetworkRequest   request , QIODevice   data )

上传内容为 data 到目的地 request and returnes a new QNetworkReply object that will be open for reply.

data must be opened for reading when this function is called and must remain valid until the finished () signal is emitted for this reply.

Whether anything will be available for reading from the returned object is protocol dependent. For HTTP, the server may send a small HTML page indicating the upload was successful (or not). Other protocols will probably have content in their replies.

注意: For HTTP, this request will send a PUT request, which most servers do not allow. Form upload mechanisms, including that of uploading files through HTML forms, use the POST mechanism.

另请参阅 get (), post (), deleteResource (), and sendCustomRequest ().

QNetworkReply QNetworkAccessManager.put ( self , QNetworkRequest   request , QByteArray   data )

这是重载函数。

发送内容为 multiPart message to the destination specified by request .

This can be used for sending MIME multipart messages over HTTP.

该函数在 Qt 4.8 引入。

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

QNetworkReply QNetworkAccessManager.put ( self , QNetworkRequest   request , QHttpMultiPart   multiPart )

这是重载函数。

发送内容为 data byte array to the destination specified by request .

QNetworkReply QNetworkAccessManager.sendCustomRequest ( self , QNetworkRequest   request , QByteArray   verb , QIODevice   data  = None)

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

用户负责发送 verb 到 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 () signal is emitted for this reply.

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

该函数在 Qt 4.7 引入。

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

QNetworkAccessManager.setCache ( self , QAbstractNetworkCache   cache )

cache argument has it's ownership transferred to Qt.

将管理器的网络缓存设为 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.

该函数在 Qt 4.5 引入。

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

QNetworkAccessManager.setConfiguration ( self , 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 () 会被使用。

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

 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 .

QNetworkAccessManager.setCookieJar ( self , QNetworkCookieJar   cookieJar )

cookieJar argument has it's ownership transferred to Qt.

将管理器的 Cookie Jar 设为 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 函数。

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 QNetworkAccessManager when it detects a new cookie.

另请参阅 cookieJar (), QNetworkCookieJar.cookiesForUrl (), and QNetworkCookieJar.setCookiesFromUrl ().

QNetworkAccessManager.setNetworkAccessible ( self , NetworkAccessibility   accessible )

QNetworkAccessManager.setProxy ( self , QNetworkProxy   proxy )

将用于未来请求的代理设为 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 ().

QNetworkAccessManager.setProxyFactory ( self , QNetworkProxyFactory   factory )

factory argument has it's ownership transferred to Qt.

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

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

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

该函数在 Qt 4.5 引入。

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


Qt Signal Documentation

void authenticationRequired (QNetworkReply*,QAuthenticator*)

This is the default overload of this signal.

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 will cache the credentials internally and will send the same values if the server requires authentication again, without emitting the authenticationRequired() signal. If it rejects the credentials, this signal will be emitted again.

注意: It is not possible to use a QueuedConnection to connect to this signal, as the connection will fail if the authenticator has not been filled in with new information when the signal returns.

另请参阅 proxyAuthenticationRequired ().

void finished (QNetworkReply*)

This is the default overload of this signal.

This signal is emitted whenever a pending network reply is finished. The reply parameter will contain a pointer to the reply that has just finished. This signal is emitted in tandem with the QNetworkReply.finished () 信号。

QNetworkReply.finished () for information on the status that the object will be in.

注意: 不要删除 reply object in the slot connected to this signal. Use deleteLater ().

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

void networkAccessibleChanged (QNetworkAccessManager::NetworkAccessibility)

This is the default overload of this signal.

This signal is emitted when the value of the networkAccessible 特性改变。 accessible is the new network accessibility.

void proxyAuthenticationRequired (const QNetworkProxy&,QAuthenticator*)

This is the default overload of this signal.

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 in the 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 will emit the signal again.

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

void sslErrors (QNetworkReply*,const QList<QSslError>&)

This is the default overload of this signal.

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 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 (), and QNetworkReply.ignoreSslErrors ().