QNetworkReply Class Reference

[ QtNetwork module]

The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager 更多...

继承 QIODevice .

类型

方法

Qt Signals


详细描述

The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager

The QNetworkReply class contains the data and meta data related to a request posted with QNetworkAccessManager 。像 QNetworkRequest , it contains a URL and headers (both in parsed and raw form), some information about the reply's state and the contents of the reply itself.

QNetworkReply 是顺序访问 QIODevice , which means that once data is read from the object, it no longer kept by the device. It is therefore the application's responsibility to keep this data if it needs to. Whenever more data is received from the network and processed, the readyRead () signal is emitted.

downloadProgress () signal is also emitted when data is received, but the number of bytes contained in it may not represent the actual bytes received, if any transformation is done to the contents (for example, decompressing and removing the protocol overhead).

即使 QNetworkReply 是 QIODevice connected to the contents of the reply, it also emits the uploadProgress () signal, which indicates the progress of the upload for operations that have such content.

注意: Do not delete the object in the slot connected to the error () 或 finished () 信号。请使用 deleteLater ().


类型文档编制

QNetworkReply.NetworkError

Indicates all possible error conditions found during the processing of the request.

常量 描述
QNetworkReply.NoError 0 没有错误条件。 注意: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest.RedirectionTargetAttribute 属性。
QNetworkReply.ConnectionRefusedError 1 the remote server refused the connection (the server is not accepting requests)
QNetworkReply.RemoteHostClosedError 2 the remote server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply.HostNotFoundError 3 the remote host name was not found (invalid hostname)
QNetworkReply.TimeoutError 4 the connection to the remote server timed out
QNetworkReply.OperationCanceledError 5 操作被取消经由调用 abort () 或 close () 在它完成之前。
QNetworkReply.SslHandshakeFailedError 6 the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors () signal should have been emitted.
QNetworkReply.TemporaryNetworkFailureError 7 the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
QNetworkReply.ProxyConnectionRefusedError 101 the connection to the proxy server was refused (the proxy server is not accepting requests)
QNetworkReply.ProxyConnectionClosedError 102 the proxy server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply.ProxyNotFoundError 103 the proxy host name was not found (invalid proxy hostname)
QNetworkReply.ProxyTimeoutError 104 the connection to the proxy timed out or the proxy did not reply in time to the request sent
QNetworkReply.ProxyAuthenticationRequiredError 105 the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
QNetworkReply.ContentAccessDenied 201 the access to the remote content was denied (similar to HTTP error 401)
QNetworkReply.ContentOperationNotPermittedError 202 the operation requested on the remote content is not permitted
QNetworkReply.ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)
QNetworkReply.AuthenticationRequiredError 204 the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
QNetworkReply.ContentReSendError 205 the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
QNetworkReply.ProtocolUnknownError 301 the Network Access API cannot honor the request because the protocol is not known
QNetworkReply.ProtocolInvalidOperationError 302 the requested operation is invalid for this protocol
QNetworkReply.UnknownNetworkError 99 an unknown network-related error was detected
QNetworkReply.UnknownProxyError 199 an unknown proxy-related error was detected
QNetworkReply.UnknownContentError 299 an unknown error related to the remote content was detected
QNetworkReply.ProtocolFailure 399 a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)

另请参阅 error ().


方法文档编制

QNetworkReply.__init__ ( self , QObject   parent  = None)

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

创建 QNetworkReply object 采用父级 parent .

You cannot directly instantiate QNetworkReply objects. Use QNetworkAccessManager functions to do that.

QNetworkReply.abort ( self )

This method is abstract and should be reimplemented in any sub-class.

Aborts the operation immediately and close down any network connections still open. Uploads still in progress are also aborted.

另请参阅 close ().

QVariant QNetworkReply.attribute ( self , QNetworkRequest.Attribute   code )

返回的属性关联代码 code 。若 the attribute has not been set, it returns an invalid QVariant (type QVariant.Null).

可以期望的默认值列表于 QNetworkRequest.Attribute 以应用由此函数返回的值。

另请参阅 setAttribute () 和 QNetworkRequest.Attribute .

QNetworkReply.close ( self )

重实现自 QIODevice.close ().

Closes this device for reading. Unread data is discarded, but the network resources are not discarded until they are finished. In particular, if any upload is in progress, it will continue until it is done.

finished () signal is emitted when all operations are over and the network resources are freed.

另请参阅 abort () and finished ().

NetworkError QNetworkReply.error ( self )

Returns the error that was found during the processing of this request. If no error was found, returns NoError .

另请参阅 setError ().

bool QNetworkReply.hasRawHeader ( self , QByteArray   headerName )

Returns true if the raw header of name headerName was sent by the remote server

另请参阅 rawHeader ().

QVariant QNetworkReply.header ( self , QNetworkRequest.KnownHeaders   header )

返回值为已知头 header , if that header was sent by the remote server. If the header was not sent, returns an invalid QVariant .

另请参阅 rawHeader (), setHeader (),和 QNetworkRequest.header ().

QNetworkReply.ignoreSslErrors ( self )

This method is also a Qt slot with the C++ signature void ignoreSslErrors() .

If this function is called, SSL errors related to network connection will be ignored, including certificate validation errors.

警告: Be sure to always let the user inspect the errors reported by the sslErrors () signal, and only call this method upon confirmation from the user that proceeding is ok. If there are unexpected errors, the reply should be aborted. Calling this method without inspecting the actual errors will most likely pose a security risk for your application. Use it with great care!

可以调用此函数从槽连接到 sslErrors () signal, which indicates which errors were found.

另请参阅 sslConfiguration (), sslErrors (),和 QSslSocket.ignoreSslErrors ().

QNetworkReply.ignoreSslErrors ( self , list-of-QSslError  errors )

这是重载函数。

若此函数被调用,SSL 错误给出于 errors 将被忽略。

注意: Because most SSL errors are associated with a certificate, for most of them you must set the expected certificate this SSL error is related to. If, for instance, you want to issue a request to a server that uses a self-signed certificate, consider the following snippet:

 QList<QSslCertificate> cert = QSslCertificate.fromPath(QLatin1String("server-certificate.pem"));
 QSslError error(QSslError.SelfSignedCertificate, cert.at(0));
 QList<QSslError> expectedSslErrors;
 expectedSslErrors.append(error);
 QNetworkReply *reply = manager.get(QNetworkRequest(QUrl("https://server.tld/index.html")));
 reply->ignoreSslErrors(expectedSslErrors);
 // here connect signals etc.
			

Multiple calls to this function will replace the list of errors that were passed in previous calls. You can clear the list of errors you want to ignore by calling this function with an empty list.

该函数在 Qt 4.6 引入。

另请参阅 sslConfiguration (), sslErrors (),和 QSslSocket.ignoreSslErrors ().

bool QNetworkReply.isFinished ( self )

Returns true when the reply has finished or was aborted.

该函数在 Qt 4.6 引入。

另请参阅 isRunning ().

bool QNetworkReply.isRunning ( self )

Returns true when the request is still processing and the reply has not finished or was aborted yet.

该函数在 Qt 4.6 引入。

另请参阅 isFinished ().

bool QNetworkReply.isSequential ( self )

QNetworkAccessManager QNetworkReply.manager ( self )

返回 QNetworkAccessManager that was used to create this QNetworkReply 对象。最初,它还是父级对象。

QNetworkAccessManager.Operation QNetworkReply.operation ( self )

返回此回复张贴的操作。

另请参阅 setOperation ().

QByteArray QNetworkReply.rawHeader ( self , QByteArray   headerName )

返回原生内容,对于 Header headerName as sent by the remote server. If there is no such header, returns an empty byte array, which may be indistinguishable from an empty header. 使用 hasRawHeader () 到 verify if the server sent such header field.

另请参阅 setRawHeader (), hasRawHeader (),和 header ().

list-of-QByteArray QNetworkReply.rawHeaderList ( self )

Returns a list of headers fields that were sent by the remote server, in the order that they were sent. Duplicate headers are merged together and take place of the latter duplicate.

list-of-tuple-of-QByteArray-QByteArray QNetworkReply.rawHeaderPairs ( self )

返回原生头对的列表。

int QNetworkReply.readBufferSize ( self )

返回读取缓冲的尺寸 (以字节为单位)。

另请参阅 setReadBufferSize ().

QNetworkRequest QNetworkReply.request ( self )

Returns the request that was posted for this reply. In special, note that the URL for the request may be different than that of the reply.

另请参阅 QNetworkRequest.url (), url (),和 setRequest ().

QNetworkReply.setAttribute ( self , QNetworkRequest.Attribute   code , QVariant  value )

设置属性 code 到拥有值 value 。若 code 先前有设置,它将被覆盖。若 value 是无效 QVariant , the attribute will be unset.

另请参阅 attribute () 和 QNetworkRequest.setAttribute ().

QNetworkReply.setError ( self , NetworkError   errorCode , QString  errorString )

将错误条件设为 errorCode 。 human-readable message is set with errorString .

Calling setError() does not emit the error( QNetworkReply.NetworkError ) 信号。

另请参阅 error () and errorString ().

QNetworkReply.setFinished ( self , bool  finished )

把回复设置为 finished .

在此设置之后,回复数据不得改变。

该函数在 Qt 4.8 引入。

另请参阅 finished () 和 isFinished ().

QNetworkReply.setHeader ( self , QNetworkRequest.KnownHeaders   header , QVariant  value )

设置已知头 header 到值 value . The corresponding raw form of the header will be set as well.

另请参阅 header (), setRawHeader (),和 QNetworkRequest.setHeader ().

QNetworkReply.setOperation ( self , QNetworkAccessManager.Operation   operation )

把此对象的关联操作设置为 operation 。该值会被返回,通过 operation ().

Note: the operation should be set when this object is created and not changed again.

另请参阅 operation () 和 setRequest ().

QNetworkReply.setRawHeader ( self , QByteArray   headerName , QByteArray   value )

设置原生头 headerName 到值 value 。若 headerName was previously set, it is overridden. Multiple HTTP headers of the same name are functionally equivalent to one single header with the values concatenated, separated by commas.

headerName 匹配已知 Header 头,值 value will be parsed and the corresponding parsed form will also be set.

另请参阅 rawHeader (), header (), setHeader (),和 QNetworkRequest.setRawHeader ().

QNetworkReply.setReadBufferSize ( self , int  size )

将读取缓冲的大小设为 size bytes. The read buffer is the buffer that holds data that is being downloaded off the network, before it is read with QIODevice.read (). Setting the buffer size to 0 will make the buffer unlimited in size.

QNetworkReply will try to stop reading from the network once this buffer is full (i.e., bytesAvailable () 返回 size or more), thus causing the download to throttle down as well. If the buffer is not limited in size, QNetworkReply will try to download as fast as possible from the network.

不像 QAbstractSocket.setReadBufferSize (), QNetworkReply cannot guarantee precision in the read buffer size. That is, bytesAvailable () can return more than size .

另请参阅 readBufferSize ().

QNetworkReply.setRequest ( self , QNetworkRequest   request )

将此对象的关联请求设为 request 。该值会被返回,通过 request ().

Note: the request should be set when this object is created and not changed again.

另请参阅 request () 和 setOperation ().

QNetworkReply.setSslConfiguration ( self , QSslConfiguration   configuration )

Sets the SSL configuration for the network connection associated with this request, if possible, to be that of config .

另请参阅 sslConfiguration ().

QNetworkReply.setUrl ( self , QUrl   url )

将正在处理的 URL 设为 url . Normally, the URL matches that of the request that was posted, but for a variety of reasons it can be different (for example, a file path being made absolute or canonical).

另请参阅 url (), request (),和 QNetworkRequest.url ().

QSslConfiguration QNetworkReply.sslConfiguration ( self )

Returns the SSL configuration and state associated with this reply, if SSL was used. It will contain the remote server's certificate, its certificate chain leading to the Certificate Authority as well as the encryption ciphers in use.

The peer's certificate and its certificate chain will be known by the time sslErrors () is emitted, if it's emitted.

另请参阅 setSslConfiguration ().

QUrl QNetworkReply.url ( self )

Returns the URL of the content downloaded or uploaded. Note that the URL may be different from that of the original request.

另请参阅 request (), setUrl (),和 QNetworkRequest.url ().

int QNetworkReply.writeData ( self , str  data )


Qt Signal Documentation

void downloadProgress (qint64,qint64)

This is the default overload of this signal.

This signal is emitted to indicate the progress of the download part of this network request, if there's any. If there's no download associated with this request, this signal will be emitted once with 0 as the value of both bytesReceived and bytesTotal .

bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal 将是 -1。

下载完成当 bytesReceived 等于 bytesTotal 。在那时, bytesTotal will not be -1.

注意:值对于 bytesReceived and bytesTotal 可能异于 size (), the total number of bytes obtained through read () 或 readAll (), or the value of the header(ContentLengthHeader). The reason for that is that there may be protocol overhead or the data may be compressed during the download.

另请参阅 uploadProgress () 和 bytesAvailable ().

void error (QNetworkReply::NetworkError)

This is the default overload of this signal.

This signal is emitted when the reply detects an error in processing. The finished () signal will probably follow, indicating that the connection is over.

code parameter contains the code of the error that was detected. Call errorString () to obtain a textual representation of the error condition.

注意: Do not delete the object in the slot connected to this signal. Use deleteLater ().

另请参阅 error () and errorString ().

void finished ()

This is the default overload of this signal.

此信号被发射,当回复已处理完成。 After this signal is emitted, there will be no more updates to the reply's data or metadata.

除非 close () has been called, the reply will be still be opened for reading, so the data can be retrieved by calls to read () 或 readAll (). In particular, if no calls to read () were made as a result of readyRead (),调用 readAll () will retrieve the full contents in a QByteArray .

此信号被串联发射采用 QNetworkAccessManager.finished () where that signal's reply parameter is this object.

注意: Do not delete the object in the slot connected to this signal. Use deleteLater ().

还可以使用 isFinished () 去校验,若 QNetworkReply has finished even before you receive the finished() signal.

另请参阅 setFinished (), QNetworkAccessManager.finished (), and isFinished ().

void metaDataChanged ()

This is the default overload of this signal.

This signal is emitted whenever the metadata in this reply changes. metadata is any information that is not the content (data) itself, including the network headers. In the majority of cases, the metadata will be known fully by the time the first byte of data is received. However, it is possible to receive updates of headers or other metadata during the processing of the data.

另请参阅 header (), rawHeaderList (), rawHeader (),和 hasRawHeader ().

void sslErrors (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 参数包含错误列表。

To indicate that the errors are not fatal and that the connection should proceed, the 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 () 到 obtain it). If the user decides to proceed after analyzing the remote certificate, the slot should call ignoreSslErrors ().

另请参阅 QSslSocket.sslErrors (), QNetworkAccessManager.sslErrors (), sslConfiguration (),和 ignoreSslErrors ().

void uploadProgress (qint64,qint64)

This is the default overload of this signal.

This signal is emitted to indicate the progress of the upload part of this network request, if there's any. If there's no upload associated with this request, this signal will not be emitted.

bytesSent parameter indicates the number of bytes uploaded, while bytesTotal indicates the total number of bytes to be uploaded. If the number of bytes to be uploaded could not be determined, bytesTotal 将是 -1。

上传完成当 bytesSent 等于 bytesTotal 。在那时, bytesTotal will not be -1.

另请参阅 downloadProgress ().