QNetworkRequest Class
The QNetworkRequest class describes a network request: a URL plus headers and transfer settings. More…
| Header | #include <QNetworkRequest> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
List of all members, including inherited members
Public Types
| enum | KnownHeaders |
| enum | RedirectPolicy |
| enum | Attribute |
Public Functions
| QNetworkRequest() | |
| QNetworkRequest(const QUrl &url) | |
| QUrl | url() const |
| void | setUrl(const QUrl &url) |
| QByteArray | header(KnownHeaders header) const |
| void | setHeader(KnownHeaders header, const QByteArray &value) |
| bool | hasRawHeader(const QByteArray &headerName) const |
| QList<QByteArray> | rawHeaderList() const |
| QByteArray | rawHeader(const QByteArray &headerName) const |
| void | setRawHeader(const QByteArray &headerName, const QByteArray &headerValue) |
| QVariant | attribute(Attribute code, const QVariant &defaultValue = QVariant()) const |
| void | setAttribute(Attribute code, const QVariant &value) |
| RedirectPolicy | redirectPolicy() const |
| void | setRedirectPolicy(RedirectPolicy policy) |
| int | maximumRedirectsAllowed() const |
| void | setMaximumRedirectsAllowed(int maximumRedirectsAllowed) |
| int | transferTimeout() const |
| void | setTransferTimeout(int timeout) |
| QSslConfiguration | sslConfiguration() const |
| void | setSslConfiguration(const QSslConfiguration &configuration) |
Detailed Description
See also QNetworkAccessManager and QNetworkReply.
Differences from Qt
Attributes are stored and returned, but none of them changes how the request is sent; setRedirectPolicy(), setMaximumRedirectsAllowed() and setTransferTimeout() cover the common settings. Known headers are read and written as QByteArray rather than QVariant. There is no priority or HTTP/2 setting, and no UserVerifiedRedirectPolicy.
Member Type Documentation
enum QNetworkRequest::Attribute
Keys for the values attached to a request with setAttribute() or reported on a reply with QNetworkReply::attribute(). Only the first group is produced by a reply; the rest are stored on a request but have no effect.
| Constant | Description |
|---|---|
QNetworkRequest::HttpStatusCodeAttribute | The HTTP status code, as an int. Reply only. |
QNetworkRequest::HttpReasonPhraseAttribute | The HTTP reason phrase, as a QByteArray. Reply only. |
QNetworkRequest::RedirectionTargetAttribute | The Location of a redirect that was not followed, as a QUrl, possibly relative. Reply only. |
QNetworkRequest::ConnectionEncryptedAttribute | true if the reply came over TLS. Reply only. |
QNetworkRequest::SourceIsFromCacheAttribute | Always false, as there is no cache. Reply only. |
QNetworkRequest::HttpPipeliningWasUsedAttribute | Always false. Reply only. |
QNetworkRequest::Http2WasUsedAttribute | Always false. Reply only. |
QNetworkRequest::CacheLoadControlAttribute | Stored only. |
QNetworkRequest::CacheSaveControlAttribute | Stored only. |
QNetworkRequest::DoNotBufferUploadDataAttribute | Stored only. |
QNetworkRequest::HttpPipeliningAllowedAttribute | Stored only. |
QNetworkRequest::CustomVerbAttribute | Stored only. |
QNetworkRequest::CookieLoadControlAttribute | Stored only. |
QNetworkRequest::AuthenticationReuseAttribute | Stored only. |
QNetworkRequest::CookieSaveControlAttribute | Stored only. |
QNetworkRequest::MaximumDownloadBufferSizeAttribute | Stored only. |
QNetworkRequest::DownloadBufferAttribute | Stored only. |
QNetworkRequest::SynchronousRequestAttribute | Stored only. |
QNetworkRequest::BackgroundRequestAttribute | Stored only. |
QNetworkRequest::EmitAllUploadProgressSignalsAttribute | Stored only. |
QNetworkRequest::Http2AllowedAttribute | Stored only. |
QNetworkRequest::OriginalContentLengthAttribute | Stored only. |
QNetworkRequest::RedirectPolicyAttribute | Stored only; use setRedirectPolicy(). |
QNetworkRequest::Http2DirectAttribute | Stored only. |
QNetworkRequest::ResourceTypeAttribute | Stored only. |
QNetworkRequest::AutoDeleteReplyOnFinishAttribute | Stored only. |
QNetworkRequest::ConnectionCacheExpiryTimeoutSecondsAttribute | Stored only. |
QNetworkRequest::Http2CleartextAllowedAttribute | Stored only. |
QNetworkRequest::UseCredentialsAttribute | Stored only. |
QNetworkRequest::User | The first code reserved for applications. |
QNetworkRequest::UserMax | The last code reserved for applications. |
enum QNetworkRequest::KnownHeaders
Common HTTP headers.
| Constant | Description |
|---|---|
QNetworkRequest::ContentTypeHeader | Content-Type |
QNetworkRequest::ContentLengthHeader | Content-Length |
QNetworkRequest::LocationHeader | Location |
QNetworkRequest::LastModifiedHeader | Last-Modified |
QNetworkRequest::CookieHeader | Cookie |
QNetworkRequest::SetCookieHeader | Set-Cookie |
QNetworkRequest::ContentDispositionHeader | Content-Disposition |
QNetworkRequest::UserAgentHeader | User-Agent |
QNetworkRequest::ServerHeader | Server |
QNetworkRequest::IfModifiedSinceHeader | If-Modified-Since |
QNetworkRequest::ETagHeader | ETag |
QNetworkRequest::IfMatchHeader | If-Match |
QNetworkRequest::IfNoneMatchHeader | If-None-Match |
enum QNetworkRequest::RedirectPolicy
Selects what happens when the server redirects a request.
| Constant | Description |
|---|---|
QNetworkRequest::ManualRedirectPolicy | Redirects are not followed; the 3xx reply is returned. |
QNetworkRequest::NoLessSafeRedirectPolicy | Redirects are followed, but not to a less secure scheme. |
QNetworkRequest::SameOriginRedirectPolicy | Only redirects to the same scheme, host and port are followed. |
Member Function Documentation
QNetworkRequest::QNetworkRequest()
Constructs a request with no URL.
QNetworkRequest::QNetworkRequest(const QUrl &url)
Constructs a request for url.
QVariant QNetworkRequest::attribute(Attribute code, const QVariant &defaultValue = QVariant()) const
Returns the value stored for code, or defaultValue if none is set.
See also setAttribute().
bool QNetworkRequest::hasRawHeader(const QByteArray &headerName) const
Returns true if the header headerName is set. Names are not case sensitive.
See also rawHeader().
QByteArray QNetworkRequest::header(KnownHeaders header) const
Returns the value of the known header, or an empty array if it is unset.
See also setHeader().
int QNetworkRequest::maximumRedirectsAllowed() const
Returns the most redirects that are followed. The default is 50.
See also setMaximumRedirectsAllowed().
QByteArray QNetworkRequest::rawHeader(const QByteArray &headerName) const
Returns the value of the header headerName, or an empty array if it is unset.
See also setRawHeader().
QList<QByteArray> QNetworkRequest::rawHeaderList() const
Returns the names of the headers that are set.
RedirectPolicy QNetworkRequest::redirectPolicy() const
Returns the redirect policy. The default is NoLessSafeRedirectPolicy.
See also setRedirectPolicy().
void QNetworkRequest::setAttribute(Attribute code, const QVariant &value)
Stores value for code, replacing any previous value. An invalid QVariant removes the attribute.
See also attribute().
void QNetworkRequest::setHeader(KnownHeaders header, const QByteArray &value)
Sets the known header to value.
See also header().
void QNetworkRequest::setMaximumRedirectsAllowed(int maximumRedirectsAllowed)
Sets the most redirects that are followed to maximumRedirectsAllowed.
See also maximumRedirectsAllowed().
void QNetworkRequest::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
Sets the header headerName to headerValue, replacing any previous value. An empty value removes the header. A name or value containing line breaks makes the request fail with ProtocolInvalidOperationError, as it could otherwise inject extra headers.
See also rawHeader().
void QNetworkRequest::setRedirectPolicy(RedirectPolicy policy)
Sets the redirect policy to policy.
See also redirectPolicy().
void QNetworkRequest::setSslConfiguration(const QSslConfiguration &configuration)
Sets the TLS settings for this request to configuration, for example to trust a private CA.
See also sslConfiguration().
void QNetworkRequest::setTransferTimeout(int timeout)
Fails the request with TimeoutError if no data moves for timeout milliseconds.
See also transferTimeout().
void QNetworkRequest::setUrl(const QUrl &url)
Sets the URL to url.
See also url().
QSslConfiguration QNetworkRequest::sslConfiguration() const
Returns the TLS settings for this request.
See also setSslConfiguration().
int QNetworkRequest::transferTimeout() const
Returns the inactivity timeout in milliseconds; 0 uses the manager's.
See also setTransferTimeout().
QUrl QNetworkRequest::url() const
Returns the URL.
See also setUrl().