
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQNetworkReply class contains the data and headers for a request sent withQNetworkAccessManagerMore...
| Header: | #include <QNetworkReply> |
| Since: | Qt 4.4 |
| Inherits: | QIODevice |
Note: All functions in this class arereentrant.
| enum | NetworkError { NoError, ConnectionRefusedError, RemoteHostClosedError, HostNotFoundError, ..., ProtocolFailure } |
| typedef | RawHeaderPair |
| ~QNetworkReply() | |
| virtual void | abort() = 0 |
| QVariant | attribute(QNetworkRequest::Attribute code) const |
| NetworkError | error() const |
| bool | hasRawHeader(const QByteArray & headerName) const |
| QVariant | header(QNetworkRequest::KnownHeaders header) const |
| void | ignoreSslErrors(const QList<QSslError> & errors) |
| bool | isFinished() const |
| bool | isRunning() const |
| QNetworkAccessManager * | manager() const |
| QNetworkAccessManager::Operation | operation() const |
| QByteArray | rawHeader(const QByteArray & headerName) const |
| QList<QByteArray> | rawHeaderList() const |
| const QList<RawHeaderPair> & | rawHeaderPairs() const |
| qint64 | readBufferSize() const |
| QNetworkRequest | request() const |
| virtual void | setReadBufferSize(qint64 size) |
| void | setSslConfiguration(const QSslConfiguration & config) |
| QSslConfiguration | sslConfiguration() const |
| QUrl | url() const |
| virtual void | close() |
| virtual void | ignoreSslErrors() |
| void | downloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
| void | error(QNetworkReply::NetworkError code) |
| void | finished() |
| void | metaDataChanged() |
| void | sslErrors(const QList<QSslError> & errors) |
| void | uploadProgress(qint64 bytesSent, qint64 bytesTotal) |
| QNetworkReply(QObject * parent = 0) | |
| void | setAttribute(QNetworkRequest::Attribute code, const QVariant & value) |
| void | setError(NetworkError errorCode, const QString & errorString) |
| void | setFinished(bool finished) |
| void | setHeader(QNetworkRequest::KnownHeaders header, const QVariant & value) |
| void | setOperation(QNetworkAccessManager::Operation operation) |
| void | setRawHeader(const QByteArray & headerName, const QByteArray & value) |
| void | setRequest(const QNetworkRequest & request) |
| void | setUrl(const QUrl & url) |
TheQNetworkReply class contains the data and headers for a request sent withQNetworkAccessManager
TheQNetworkReply class contains the data and meta data related to a request posted withQNetworkAccessManager. LikeQNetworkRequest, 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 is a sequential-accessQIODevice, 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, thereadyRead() signal is emitted.
ThedownloadProgress() 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).
Even thoughQNetworkReply is aQIODevice connected to the contents of the reply, it also emits theuploadProgress() signal, which indicates the progress of the upload for operations that have such content.
Note:Do not delete the object in the slot connected to theerror() orfinished() signal. UsedeleteLater().
See alsoQNetworkRequest andQNetworkAccessManager.
Indicates all possible error conditions found during the processing of the request.
| Constant | Value | Description |
|---|---|---|
QNetworkReply::NoError | 0 | no error condition. |
Note:When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with theQNetworkRequest::RedirectionTargetAttribute attribute.
| Constant | Value | Description |
|---|---|---|
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 | the operation was canceled via calls toabort() orclose() before it was finished. |
QNetworkReply::SslHandshakeFailedError | 6 | the SSL/TLS handshake failed and the encrypted channel could not be established. ThesslErrors() 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.) |
See alsoerror().
RawHeaderPair is aQPair<QByteArray,QByteArray> where the firstQByteArray is the header name and the second is the header.
[protected]QNetworkReply::QNetworkReply(QObject * parent = 0)Creates aQNetworkReply object with parentparent.
You cannot directly instantiateQNetworkReply objects. UseQNetworkAccessManager functions to do that.
Disposes of this reply and frees any resources associated with it. If any network connections are still open, they will be closed.
[pure virtual]void QNetworkReply::abort()Aborts the operation immediately and close down any network connections still open. Uploads still in progress are also aborted.
See alsoclose().
Returns the attribute associated with the codecode. If the attribute has not been set, it returns an invalidQVariant (type QVariant::Null).
You can expect the default values listed inQNetworkRequest::Attribute to be applied to the values returned by this function.
See alsosetAttribute() andQNetworkRequest::Attribute.
[virtual]void QNetworkReply::close()Reimplemented fromQIODevice::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.
Thefinished() signal is emitted when all operations are over and the network resources are freed.
See alsoabort() andfinished().
[signal]void QNetworkReply::downloadProgress(qint64 bytesReceived,qint64 bytesTotal)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 bothbytesReceived andbytesTotal.
ThebytesReceived parameter indicates the number of bytes received, whilebytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known,bytesTotal will be -1.
The download is finished whenbytesReceived is equal tobytesTotal. At that time,bytesTotal will not be -1.
Note that the values of bothbytesReceived andbytesTotal may be different fromsize(), the total number of bytes obtained throughread() orreadAll(), 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.
See alsouploadProgress() andbytesAvailable().
Returns the error that was found during the processing of this request. If no error was found, returnsNoError.
See alsosetError().
[signal]void QNetworkReply::error(QNetworkReply::NetworkError code)This signal is emitted when the reply detects an error in processing. Thefinished() signal will probably follow, indicating that the connection is over.
Thecode parameter contains the code of the error that was detected. CallerrorString() to obtain a textual representation of the error condition.
Note:Do not delete the object in the slot connected to this signal. UsedeleteLater().
Note:Signalerror is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(networkReply,static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),[=](QNetworkReply::NetworkError code){/* ... */ });
See alsoerror() anderrorString().
[signal]void QNetworkReply::finished()This signal is emitted when the reply has finished processing. After this signal is emitted, there will be no more updates to the reply's data or metadata.
Unlessclose() has been called, the reply will be still be opened for reading, so the data can be retrieved by calls toread() orreadAll(). In particular, if no calls toread() were made as a result ofreadyRead(), a call toreadAll() will retrieve the full contents in aQByteArray.
This signal is emitted in tandem withQNetworkAccessManager::finished() where that signal's reply parameter is this object.
Note:Do not delete the object in the slot connected to this signal. UsedeleteLater().
You can also useisFinished() to check if aQNetworkReply has finished even before you receive the finished() signal.
See alsosetFinished(),QNetworkAccessManager::finished(), andisFinished().
Returns true if the raw header of nameheaderName was sent by the remote server
See alsorawHeader().
Returns the value of the known headerheader, if that header was sent by the remote server. If the header was not sent, returns an invalidQVariant.
See alsorawHeader(),setHeader(), andQNetworkRequest::header().
[virtual slot]void QNetworkReply::ignoreSslErrors()If this function is called, SSL errors related to network connection will be ignored, including certificate validation errors.
Warning: Be sure to always let the user inspect the errors reported by thesslErrors() 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!
This function can be called from the slot connected to thesslErrors() signal, which indicates which errors were found.
See alsosslConfiguration(),sslErrors(), andQSslSocket::ignoreSslErrors().
This is an overloaded function.
If this function is called, the SSL errors given inerrors will be ignored.
Note: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"));QSslErrorerror(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.
This function was introduced in Qt 4.6.
See alsosslConfiguration(),sslErrors(), andQSslSocket::ignoreSslErrors().
Returns true when the reply has finished or was aborted.
This function was introduced in Qt 4.6.
See alsoisRunning().
Returns true when the request is still processing and the reply has not finished or was aborted yet.
This function was introduced in Qt 4.6.
See alsoisFinished().
Returns theQNetworkAccessManager that was used to create thisQNetworkReply object. Initially, it is also the parent object.
[signal]void QNetworkReply::metaDataChanged()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.
See alsoheader(),rawHeaderList(),rawHeader(), andhasRawHeader().
Returns the operation that was posted for this reply.
See alsosetOperation().
Returns the raw contents of the headerheaderName 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. UsehasRawHeader() to verify if the server sent such header field.
See alsosetRawHeader(),hasRawHeader(), andheader().
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.
Returns a list of raw header pairs.
Returns the size of the read buffer, in bytes.
See alsosetReadBufferSize().
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.
See alsoQNetworkRequest::url(),url(), andsetRequest().
[protected]void QNetworkReply::setAttribute(QNetworkRequest::Attribute code, constQVariant & value)Sets the attributecode to have valuevalue. Ifcode was previously set, it will be overridden. Ifvalue is an invalidQVariant, the attribute will be unset.
See alsoattribute() andQNetworkRequest::setAttribute().
[protected]void QNetworkReply::setError(NetworkError errorCode, constQString & errorString)Sets the error condition to beerrorCode. The human-readable message is set witherrorString.
Calling setError() does not emit the error(QNetworkReply::NetworkError) signal.
See alsoerror() anderrorString().
[protected]void QNetworkReply::setFinished(bool finished)Sets the reply asfinished.
After having this set the replies data must not change.
This function was introduced in Qt 4.8.
See alsofinished() andisFinished().
[protected]void QNetworkReply::setHeader(QNetworkRequest::KnownHeaders header, constQVariant & value)Sets the known headerheader to be of valuevalue. The corresponding raw form of the header will be set as well.
See alsoheader(),setRawHeader(), andQNetworkRequest::setHeader().
[protected]void QNetworkReply::setOperation(QNetworkAccessManager::Operation operation)Sets the associated operation for this object to beoperation. This value will be returned byoperation().
Note: the operation should be set when this object is created and not changed again.
See alsooperation() andsetRequest().
[protected]void QNetworkReply::setRawHeader(constQByteArray & headerName, constQByteArray & value)Sets the raw headerheaderName to be of valuevalue. IfheaderName 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.
IfheaderName matches a known header, the valuevalue will be parsed and the corresponding parsed form will also be set.
See alsorawHeader(),header(),setHeader(), andQNetworkRequest::setRawHeader().
[virtual]void QNetworkReply::setReadBufferSize(qint64 size)Sets the size of the read buffer to besize bytes. The read buffer is the buffer that holds data that is being downloaded off the network, before it is read withQIODevice::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() returnssize 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.
UnlikeQAbstractSocket::setReadBufferSize(),QNetworkReply cannot guarantee precision in the read buffer size. That is,bytesAvailable() can return more thansize.
See alsoreadBufferSize().
[protected]void QNetworkReply::setRequest(constQNetworkRequest & request)Sets the associated request for this object to berequest. This value will be returned byrequest().
Note: the request should be set when this object is created and not changed again.
See alsorequest() andsetOperation().
Sets the SSL configuration for the network connection associated with this request, if possible, to be that ofconfig.
See alsosslConfiguration().
[protected]void QNetworkReply::setUrl(constQUrl & url)Sets the URL being processed to beurl. 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).
See alsourl(),request(), andQNetworkRequest::url().
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 timesslErrors() is emitted, if it's emitted.
See alsosetSslConfiguration().
[signal]void QNetworkReply::sslErrors(constQList<QSslError> & errors)This signal is emitted if the SSL/TLS session encountered errors during the set up, including certificate verification errors. Theerrors parameter contains the list of errors.
To indicate that the errors are not fatal and that the connection should proceed, theignoreSslErrors() 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 (seesslConfiguration() to obtain it). If the user decides to proceed after analyzing the remote certificate, the slot should callignoreSslErrors().
See alsoQSslSocket::sslErrors(),QNetworkAccessManager::sslErrors(),sslConfiguration(), andignoreSslErrors().
[signal]void QNetworkReply::uploadProgress(qint64 bytesSent,qint64 bytesTotal)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.
ThebytesSent parameter indicates the number of bytes uploaded, whilebytesTotal indicates the total number of bytes to be uploaded. If the number of bytes to be uploaded could not be determined,bytesTotal will be -1.
The upload is finished whenbytesSent is equal tobytesTotal. At that time,bytesTotal will not be -1.
See alsodownloadProgress().
Returns the URL of the content downloaded or uploaded. Note that the URL may be different from that of the original request.
See alsorequest(),setUrl(), andQNetworkRequest::url().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.