QNetworkReply Class
The QNetworkReply class holds the response to a QNetworkAccessManager request. More…
| Header | #include <QNetworkReply> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Inherits | QIODevice |
List of all members, including inherited members
Public Types
| enum | NetworkError |
Public Functions
| ~QNetworkReply() | |
| QUrl | url() const |
| QNetworkRequest | request() const |
| QNetworkAccessManager * | manager() const |
| NetworkError | error() const |
| bool | isFinished() const |
| bool | isRunning() const |
| int | httpStatusCode() const |
| QByteArray | httpReasonPhrase() const |
| QVariant | attribute(QNetworkRequest::Attribute code) const |
| QByteArray | header(QNetworkRequest::KnownHeaders header) const |
| bool | hasRawHeader(const QByteArray &headerName) const |
| QList<QByteArray> | rawHeaderList() const |
| QByteArray | rawHeader(const QByteArray &headerName) const |
| QList<std::pair<QByteArray, QByteArray>> | rawHeaderPairs() const |
| QSslConfiguration | sslConfiguration() const |
| virtual void | ignoreSslErrors(const QList<QSslError> &errors) |
Reimplemented Public Functions
| bool | isSequential() const |
| qint64 | bytesAvailable() const |
| bool | canReadLine() const |
| bool | atEnd() const |
| void | close() |
Public Slots
| virtual void | abort() |
| virtual void | ignoreSslErrors() |
Signals
| void | finished() |
| void | errorOccurred(QNetworkReply::NetworkError code) |
| void | metaDataChanged() |
| void | redirected(const QUrl &url) |
| void | uploadProgress(qint64 bytesSent, qint64 bytesTotal) |
| void | downloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
| void | encrypted() |
| void | sslErrors(const QList<QSslError> &errors) |
Protected Functions
| QNetworkReply(QObject *parent = nullptr) |
Reimplemented Protected Functions
Detailed Description
A reply is a sequential QIODevice. Data becomes readable as it arrives, and finished() is emitted once at the end, on success or failure. An HTTP error status such as 404 sets error(), and its body stays readable.
See also QNetworkAccessManager and QNetworkRequest.
Differences from Qt
Only the attributes listed under QNetworkRequest::Attribute as reply attributes are produced; the others read back what the request stored. header() returns QByteArray. Unlike Qt, the whole response stays buffered until read, with no read buffer limit. There are no proxy errors, as proxies do not exist yet.
Member Type Documentation
enum QNetworkReply::NetworkError
Describes how a reply failed.
| Constant | Description |
|---|---|
QNetworkReply::NoError | The request succeeded. |
QNetworkReply::ConnectionRefusedError | The server refused the connection. |
QNetworkReply::RemoteHostClosedError | The server closed the connection before the reply was complete. |
QNetworkReply::HostNotFoundError | The host name could not be resolved. |
QNetworkReply::TimeoutError | The transfer timed out. |
QNetworkReply::OperationCanceledError | abort() was called. |
QNetworkReply::SslHandshakeFailedError | The TLS handshake failed, or the server could not be verified. |
QNetworkReply::TemporaryNetworkFailureError | Not used. |
QNetworkReply::NetworkSessionFailedError | Not used. |
QNetworkReply::BackgroundRequestNotAllowedError | Not used. |
QNetworkReply::TooManyRedirectsError | The redirect limit was reached. |
QNetworkReply::InsecureRedirectError | The redirect policy refused a redirect. |
QNetworkReply::UnknownNetworkError | Another network error occurred. |
QNetworkReply::ContentAccessDenied | The server replied 403. |
QNetworkReply::ContentOperationNotPermittedError | The server replied 405. |
QNetworkReply::ContentNotFoundError | The server replied 404. |
QNetworkReply::AuthenticationRequiredError | The server replied 401. |
QNetworkReply::ContentReSendError | Not used. |
QNetworkReply::ContentConflictError | The server replied 409. |
QNetworkReply::ContentGoneError | The server replied 410. |
QNetworkReply::UnknownContentError | The server replied with another 4xx status. |
QNetworkReply::ProtocolUnknownError | The URL's scheme is not supported, or the URL is invalid. |
QNetworkReply::ProtocolInvalidOperationError | The request itself is malformed. |
QNetworkReply::ProtocolFailure | The server sent an invalid response. |
QNetworkReply::InternalServerError | The server replied 500. |
QNetworkReply::OperationNotImplementedError | The server replied 501. |
QNetworkReply::ServiceUnavailableError | The server replied 503. |
QNetworkReply::UnknownServerError | The server replied with another 5xx status. |
Member Function Documentation
QNetworkReply::QNetworkReply(QObject *parent = nullptr)
Constructs a reply with the given parent.
QNetworkReply::~QNetworkReply()
Destroys the reply, abandoning the transfer if it is still running.
void QNetworkReply::abort()
Stops the transfer. The reply finishes with OperationCanceledError.
bool QNetworkReply::atEnd() const
Reimplements QIODevice::atEnd().
QVariant QNetworkReply::attribute(QNetworkRequest::Attribute code) const
Returns the attribute for code, or an invalid QVariant if it has none. Status, reason, redirect target and encryption are taken from the response; any other code returns what the request stored with QNetworkRequest::setAttribute(), which is how applications tag a request and find the tag again in the reply.
See also httpStatusCode() and request().
qint64 QNetworkReply::bytesAvailable() const
Reimplements QIODevice::bytesAvailable().
bool QNetworkReply::canReadLine() const
Reimplements QIODevice::canReadLine().
void QNetworkReply::close()
Reimplements QIODevice::close().
void QNetworkReply::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
This signal is emitted as data arrives, with bytesReceived of bytesTotal bytes done. bytesTotal is -1 if the size is unknown.
void QNetworkReply::encrypted()
This signal is emitted when the TLS handshake has finished.
NetworkError QNetworkReply::error() const
Returns the error, or NoError if there has been none.
See also errorOccurred().
void QNetworkReply::errorOccurred(QNetworkReply::NetworkError code)
This signal is emitted just before finished() when the reply fails with code.
void QNetworkReply::finished()
This signal is emitted when the reply has finished, whether it succeeded or not. It is emitted exactly once.
bool QNetworkReply::hasRawHeader(const QByteArray &headerName) const
Returns true if the response has the header headerName. Names are not case sensitive.
QByteArray QNetworkReply::header(QNetworkRequest::KnownHeaders header) const
Returns the value of the known header.
See also rawHeader().
QByteArray QNetworkReply::httpReasonPhrase() const
Returns the HTTP reason phrase.
See also httpStatusCode().
int QNetworkReply::httpStatusCode() const
Returns the HTTP status code, or 0 if none has been received.
See also httpReasonPhrase().
void QNetworkReply::ignoreSslErrors(const QList<QSslError> &errors)
Continues despite the errors listed, and no others. Call this before the handshake or from a slot connected to sslErrors(). It does not carry over to a redirect that leaves the origin.
void QNetworkReply::ignoreSslErrors()
This is an overloaded function.
Continues despite every verification problem. Call this from a slot connected to sslErrors(). It removes the protection TLS gives against impersonation, and does not carry over to a redirect that leaves the origin.
bool QNetworkReply::isFinished() const
Returns true once the reply has finished.
See also finished() and isRunning().
bool QNetworkReply::isRunning() const
Returns true while the request is in progress.
See also isFinished().
bool QNetworkReply::isSequential() const
Reimplements QIODevice::isSequential().
QNetworkAccessManager * QNetworkReply::manager() const
Returns the manager that created the reply.
void QNetworkReply::metaDataChanged()
This signal is emitted when the response headers have been received.
QByteArray QNetworkReply::rawHeader(const QByteArray &headerName) const
Returns the value of the header headerName. Repeated headers are joined with ", ", or with a newline for Set-Cookie.
QList<QByteArray> QNetworkReply::rawHeaderList() const
Returns the names of the response headers.
QList<std::pair<QByteArray, QByteArray>> QNetworkReply::rawHeaderPairs() const
Returns the response headers in the order they were received.
qint64 QNetworkReply::readData(char *data, qint64 maxSize)
Reimplements QIODevice::readData().
void QNetworkReply::redirected(const QUrl &url)
This signal is emitted when a redirect to url is followed.
QNetworkRequest QNetworkReply::request() const
Returns the request this reply answers.
QSslConfiguration QNetworkReply::sslConfiguration() const
Returns the TLS details of the connection, once the handshake has finished.
void QNetworkReply::sslErrors(const QList<QSslError> &errors)
This signal is emitted if verifying the server found errors. Unless a connected slot calls ignoreSslErrors(), the reply fails.
void QNetworkReply::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
This signal is emitted as the body is sent, with bytesSent of bytesTotal bytes done.
QUrl QNetworkReply::url() const
Returns the URL of the content that was fetched, which differs from the request's URL after a redirect.
See also request().
qint64 QNetworkReply::writeData(const char *data, qint64 maxSize)
Reimplements QIODevice::writeData().