QNetworkRequest 类保持要被发送的请求,采用 QNetworkAccessManager . 更多...
头: | #include <QNetworkRequest> |
CMake: |
find_package(Qt6 COMPONENTS Network REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Network) |
qmake: | QT += network |
enum | Attribute { HttpStatusCodeAttribute, HttpReasonPhraseAttribute, RedirectionTargetAttribute, ConnectionEncryptedAttribute, CacheLoadControlAttribute, …, UserMax } |
enum | CacheLoadControl { AlwaysNetwork, PreferNetwork, PreferCache, AlwaysCache } |
enum | KnownHeaders { ContentDispositionHeader, ContentTypeHeader, ContentLengthHeader, LocationHeader, LastModifiedHeader, …, ServerHeader } |
enum | LoadControl { Automatic, Manual } |
enum | Priority { HighPriority, NormalPriority, LowPriority } |
enum | RedirectPolicy { ManualRedirectPolicy, NoLessSafeRedirectPolicy, SameOriginRedirectPolicy, UserVerifiedRedirectPolicy } |
enum | TransferTimeoutConstant { DefaultTransferTimeoutConstant } |
QNetworkRequest (const QNetworkRequest & other ) | |
QNetworkRequest (const QUrl & url ) | |
QNetworkRequest () | |
QNetworkRequest & | operator= (const QNetworkRequest & other ) |
~QNetworkRequest () | |
QVariant | attribute (QNetworkRequest::Attribute code , const QVariant & defaultValue = QVariant()) const |
qint64 | decompressedSafetyCheckThreshold () const |
bool | hasRawHeader (const QByteArray & headerName ) const |
QVariant | header (QNetworkRequest::KnownHeaders header ) const |
QHttp2Configuration | http2Configuration () const |
int | maximumRedirectsAllowed () const |
QObject * | originatingObject () const |
QString | peerVerifyName () const |
QNetworkRequest::Priority | priority () const |
QByteArray | rawHeader (const QByteArray & headerName ) const |
QList<QByteArray> | rawHeaderList () const |
void | setAttribute (QNetworkRequest::Attribute code , const QVariant & value ) |
void | setDecompressedSafetyCheckThreshold (qint64 threshold ) |
void | setHeader (QNetworkRequest::KnownHeaders header , const QVariant & value ) |
void | setHttp2Configuration (const QHttp2Configuration & configuration ) |
void | setMaximumRedirectsAllowed (int maxRedirectsAllowed ) |
void | setOriginatingObject (QObject * object ) |
void | setPeerVerifyName (const QString & peerName ) |
void | setPriority (QNetworkRequest::Priority priority ) |
void | setRawHeader (const QByteArray & headerName , const QByteArray & headerValue ) |
void | setSslConfiguration (const QSslConfiguration & config ) |
void | setTransferTimeout (int timeout = DefaultTransferTimeoutConstant) |
void | setUrl (const QUrl & url ) |
QSslConfiguration | sslConfiguration () const |
void | swap (QNetworkRequest & other ) |
int | transferTimeout () const |
QUrl | url () const |
bool | operator!= (const QNetworkRequest & other ) const |
bool | operator== (const QNetworkRequest & other ) const |
QNetworkRequest 属于网络访问 API 且是保持通过网络发送必要请求信息的类。它包含 URL 和一些可用于修改请求的辅助信息。
另请参阅 QNetworkReply and QNetworkAccessManager .
属性代码用于 QNetworkRequest and QNetworkReply .
Attributes are extra meta-data that are used to control the behavior of the request and to pass further information from the reply back to the application. Attributes are also extensible, allowing custom implementations to pass custom values.
The following table explains what the default attribute codes are, the QVariant types associated, the default value if said attribute is missing and whether it's used in requests or replies.
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::HttpStatusCodeAttribute
|
0
|
仅回复,类型: QMetaType::Int (no default) Indicates the HTTP status code received from the HTTP server (like 200, 304, 404, 401, etc.). If the connection was not HTTP-based, this attribute will not be present. |
QNetworkRequest::HttpReasonPhraseAttribute
|
1
|
仅回复,类型: QMetaType::QByteArray (no default) Indicates the HTTP reason phrase as received from the HTTP server (like "Ok", "Found", "Not Found", "Access Denied", etc.) This is the human-readable representation of the status code (see above). If the connection was not HTTP-based, this attribute will not be present. |
QNetworkRequest::RedirectionTargetAttribute
|
2
|
仅回复,类型: QMetaType::QUrl (no default) If present, it indicates that the server is redirecting the request to a different URL. The Network Access API does follow redirections by default, but if QNetworkRequest::ManualRedirectPolicy is enabled and the redirect was not handled in redirected() then this attribute will be present. The returned URL might be relative. Use QUrl::resolved () to create an absolute URL out of it. |
QNetworkRequest::ConnectionEncryptedAttribute
|
3
|
仅回复,类型: QMetaType::Bool (default: false) Indicates whether the data was obtained through an encrypted (secure) connection. |
QNetworkRequest::CacheLoadControlAttribute
|
4
|
仅请求,类型: QMetaType::Int (default: QNetworkRequest::PreferNetwork ) Controls how the cache should be accessed. The possible values are those of QNetworkRequest::CacheLoadControl . Note that the default QNetworkAccessManager implementation does not support caching. However, this attribute may be used by certain backends to modify their requests (for example, for caching proxies). |
QNetworkRequest::CacheSaveControlAttribute
|
5
|
仅请求,类型: QMetaType::Bool (default: true) Controls if the data obtained should be saved to cache for future uses. If the value is false, the data obtained will not be automatically cached. If true, data may be cached, provided it is cacheable (what is cacheable depends on the protocol being used). |
QNetworkRequest::SourceIsFromCacheAttribute
|
6
|
仅回复,类型: QMetaType::Bool (default: false) Indicates whether the data was obtained from cache or not. |
QNetworkRequest::DoNotBufferUploadDataAttribute
|
7
|
仅请求,类型: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to buffer the upload data, e.g. when doing a HTTP POST. When using this flag with sequential upload data, the ContentLengthHeader header must be set. |
QNetworkRequest::HttpPipeliningAllowedAttribute
|
8
|
仅请求,类型: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP pipelining with this request. |
QNetworkRequest::HttpPipeliningWasUsedAttribute
|
9
|
仅回复,类型: QMetaType::Bool Indicates whether the HTTP pipelining was used for receiving this reply. |
QNetworkRequest::CustomVerbAttribute
|
10
|
仅请求,类型: QMetaType::QByteArray Holds the value for the custom HTTP verb to send (destined for usage of other verbs than GET, POST, PUT and DELETE). This verb is set when calling QNetworkAccessManager::sendCustomRequest (). |
QNetworkRequest::CookieLoadControlAttribute
|
11
|
仅请求,类型: QMetaType::Int (default: QNetworkRequest::Automatic ) Indicates whether to send 'Cookie' headers in the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
QNetworkRequest::CookieSaveControlAttribute
|
13
|
仅请求,类型: QMetaType::Int (default: QNetworkRequest::Automatic ) Indicates whether to save 'Cookie' headers received from the server in reply to the request. This attribute is set to false by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
QNetworkRequest::AuthenticationReuseAttribute
|
12
|
仅请求,类型: QMetaType::Int (default: QNetworkRequest::Automatic ) Indicates whether to use cached authorization credentials in the request, if available. If this is set to QNetworkRequest::Manual and the authentication mechanism is 'Basic' or 'Digest', Qt will not send an an 'Authorization' HTTP header with any cached credentials it may have for the request's URL. This attribute is set to QNetworkRequest::Manual by Qt WebKit when creating a cross-origin XMLHttpRequest where withCredentials has not been set explicitly to true by the Javascript that created the request. See here for more information. (This value was introduced in 4.7.) |
QNetworkRequest::BackgroundRequestAttribute
|
17
|
类型: QMetaType::Bool (default: false) Indicates that this is a background transfer, rather than a user initiated transfer. Depending on the platform, background transfers may be subject to different policies. |
QNetworkRequest::Http2AllowedAttribute
|
19
|
仅请求,类型: QMetaType::Bool (default: true) Indicates whether the QNetworkAccessManager code is allowed to use HTTP/2 with this request. This applies to SSL requests or 'cleartext' HTTP/2. |
QNetworkRequest::Http2WasUsedAttribute
|
20
|
仅回复,类型: QMetaType::Bool (default: false) Indicates whether HTTP/2 was used for receiving this reply. (This value was introduced in 5.9.) |
QNetworkRequest::EmitAllUploadProgressSignalsAttribute
|
18
|
仅请求,类型: QMetaType::Bool (default: false) Indicates whether all upload signals should be emitted. By default, the uploadProgress signal is emitted only in 100 millisecond intervals. (This value was introduced in 5.5.) |
QNetworkRequest::OriginalContentLengthAttribute
|
21
|
仅回复,类型 QMetaType::Int Holds the original content-length attribute before being invalidated and removed from the header when the data is compressed and the request was marked to be decompressed automatically. (This value was introduced in 5.9.) |
QNetworkRequest::RedirectPolicyAttribute
|
22
|
仅请求,类型: QMetaType::Int , should be one of the QNetworkRequest::RedirectPolicy values (default: NoLessSafeRedirectPolicy ). (This value was introduced in 5.9.) |
QNetworkRequest::Http2DirectAttribute
|
23
|
仅请求,类型: QMetaType::Bool (default: false) If set, this attribute will force QNetworkAccessManager to use HTTP/2 protocol without initial HTTP/2 protocol negotiation. Use of this attribute implies prior knowledge that a particular server supports HTTP/2. The attribute works with SSL or 'cleartext' HTTP/2. If a server turns out to not support HTTP/2, when HTTP/2 direct was specified, QNetworkAccessManager gives up, without attempting to fall back to HTTP/1.1. If both Http2AllowedAttribute and Http2DirectAttribute are set, Http2DirectAttribute takes priority. (This value was introduced in 5.11.) |
QNetworkRequest::AutoDeleteReplyOnFinishAttribute
|
25
|
仅请求,类型: QMetaType::Bool (default: false) If set, this attribute will make QNetworkAccessManager delete the QNetworkReply after having emitted "finished". (This value was introduced in 5.14.) |
QNetworkRequest::User
|
1000
|
Special type. Additional information can be passed in QVariants with types ranging from User to UserMax. The default implementation of Network Access will ignore any request attributes in this range and it will not produce any attributes in this range in replies. The range is reserved for extensions of QNetworkAccessManager . |
QNetworkRequest::UserMax
|
32767
|
特殊类型。见 User。 |
控制缓存机制为 QNetworkAccessManager .
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::AlwaysNetwork
|
0
|
always load from network and do not check if the cache has a valid entry (similar to the "Reload" feature in browsers); in addition, force intermediate caches to re-validate. |
QNetworkRequest::PreferNetwork
|
1
|
default value; load from the network if the cached entry is older than the network entry. This will never return stale data from the cache, but revalidate resources that have become stale. |
QNetworkRequest::PreferCache
|
2
|
load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache. |
QNetworkRequest::AlwaysCache
|
3
|
仅从缓存加载,指示出错若项未缓存 (即:离线模式) |
已知头类型列表 QNetworkRequest 剖析。每个已知头也以其完整 HTTP 名称按原生形式表示。
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::ContentDispositionHeader
|
6
|
相当于 HTTP Content-Disposition 头且包含包含处置类型 (例如:附件) 及参数 (例如:文件名) 的字符串。 |
QNetworkRequest::ContentTypeHeader
|
0
|
相当于 HTTP Content-Type 头且包含包含媒体 MIME 类型及任何辅助数据 (例如:字符集) 的字符串。 |
QNetworkRequest::ContentLengthHeader
|
1
|
相当于 HTTP Content-Length 头且包含以字节为单位的传输数据长度。 |
QNetworkRequest::LocationHeader
|
2
|
相当于 HTTP Location 头且包含表示数据实际位置的 URL,包括重定向情况下的目的地 URL。 |
QNetworkRequest::LastModifiedHeader
|
3
|
相当于 HTTP Last-Modified 头且包含 QDateTime 表示内容的最后修改日期。 |
QNetworkRequest::IfModifiedSinceHeader
|
9
|
相当于 HTTP If-Modified-Since 头且包含 QDateTime 。它通常被添加到 QNetworkRequest 。服务器应发送 304 (未修改) 响应,若资源从此时间起未改变。 |
QNetworkRequest::ETagHeader
|
10
|
相当于 HTTP ETag 头且包含 QString 表示内容的最后修改状态。 |
QNetworkRequest::IfMatchHeader
|
11
|
相当于 HTTP If-Match 头且包含 QStringList 。它通常被添加到 QNetworkRequest 。服务器应发送 412 (前提条件失败) 响应,若资源不匹配。 |
QNetworkRequest::IfNoneMatchHeader
|
12
|
相当于 HTTP If-None-Match 头且包含 QStringList 。它通常被添加到 QNetworkRequest 。服务器应发送 304 (未修改) 响应,若资源匹配。 |
QNetworkRequest::CookieHeader
|
4
|
相当于 HTTP Cookie 头且包含 QList < QNetworkCookie > 表示要发送回服务器的 Cookie。 |
QNetworkRequest::SetCookieHeader
|
5
|
相当于 HTTP Set-Cookie 头且包含 QList < QNetworkCookie > 表示由服务器发送给本地存储的 Cookie。 |
QNetworkRequest::UserAgentHeader
|
7
|
由 HTTP 客户端发送的 User-Agent 头。 |
QNetworkRequest::ServerHeader
|
8
|
由 HTTP 客户端收到的服务器 Header 头。 |
另请参阅 header (), setHeader (), rawHeader (),和 setRawHeader ().
指示是否已手动覆盖请求的加载机制的某个方面,如,通过 Qt WebKit。
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::Automatic
|
0
|
默认值:指示默认行为。 |
QNetworkRequest::Manual
|
1
|
指示已手动覆写行为。 |
此枚举列表可能的网络请求优先级。
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::HighPriority
|
1
|
高优先级 |
QNetworkRequest::NormalPriority
|
3
|
正常优先级 |
QNetworkRequest::LowPriority
|
5
|
低优先级 |
[since 5.9]
enum QNetworkRequest::
RedirectPolicy
指示网络访问 API 是否应该自动遵循 HTTP 重定向响应。
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::ManualRedirectPolicy
|
0
|
Not following any redirects. |
QNetworkRequest::NoLessSafeRedirectPolicy
|
1
|
Default value: Only "http"->"http", "http" -> "https" or "https" -> "https" redirects are allowed. |
QNetworkRequest::SameOriginRedirectPolicy
|
2
|
要求相同协议、主机及端口。注意:采用此策略 http://example.com 和 http://example.com:80 将失败 (隐式/明确端口被认为是不匹配的)。 |
QNetworkRequest::UserVerifiedRedirectPolicy
|
3
|
客户端决定是否遵循每个重定向通过处理 redirected() 信号,发射 redirectAllowed() 当 QNetworkReply 对象允许重定向或中止/完成以拒绝重定向。这可以被使用,例如,询问用户是否接受重定向,或基于某些特定 APP 配置来决定。 |
该枚举在 Qt 5.9 引入或被修改。
[since 5.15]
enum QNetworkRequest::
TransferTimeoutConstant
可以被用于启用带预置值的传输超时的常量。
常量 | 值 | 描述 |
---|---|---|
QNetworkRequest::DefaultTransferTimeoutConstant
|
30000
|
传输超时 (以毫秒为单位)。使用,若不带自变量调用 setTimeout()。 |
该枚举在 Qt 5.15 引入或被修改。
创建副本为 other .
构造 QNetworkRequest 对象采用 url 作为要请求的 URL。
构造不带要请求 URL 的 QNetworkRequest 对象。使用 setUrl () 去设置一个。
创建副本为 other
Disposes of the QNetworkRequest 对象。
返回属性关联代码 code . If the attribute has not been set, it returns defaultValue .
注意: 此函数不应用默认,列出于 QNetworkRequest::Attribute .
另请参阅 setAttribute () 和 QNetworkRequest::Attribute .
[since 6.2]
qint64
QNetworkRequest::
decompressedSafetyCheckThreshold
() const
Returns the threshold for archive bomb checks.
If the decompressed size of a reply is smaller than this, Qt will simply decompress it, without further checking.
该函数在 Qt 6.2 引入。
另请参阅 setDecompressedSafetyCheckThreshold ().
返回
true
若原生头
headerName
呈现在此网络请求中。
另请参阅 rawHeader () 和 setRawHeader ().
返回已知网络 Header 头的值为 header 若它呈现在此请求中。若不存在,返回 QVariant() (即:无效变体)。
另请参阅 KnownHeaders , rawHeader (),和 setHeader ().
[since 5.14]
QHttp2Configuration
QNetworkRequest::
http2Configuration
() const
Returns the current parameters that QNetworkAccessManager is using for this request and its underlying HTTP/2 connection. This is either a configuration previously set by an application or a default configuration.
The default values that QNetworkAccessManager is using are:
By default, server push is disabled, Huffman compression and string indexing are enabled.
该函数在 Qt 5.14 引入。
另请参阅 setHttp2Configuration .
[since 5.6]
int
QNetworkRequest::
maximumRedirectsAllowed
() const
Returns the maximum number of redirects allowed to be followed for this request.
该函数在 Qt 5.6 引入。
另请参阅 setMaximumRedirectsAllowed ().
Returns a reference to the object that initiated this network request; returns
nullptr
if not set or the object has been destroyed.
另请参阅 setOriginatingObject ().
[since 5.13]
QString
QNetworkRequest::
peerVerifyName
() const
Returns the host name set for the certificate validation, as set by setPeerVerifyName . By default this returns a null string.
该函数在 Qt 5.13 引入。
另请参阅 setPeerVerifyName .
返回此请求的优先级。
另请参阅 setPriority ().
返回 Header 头的原生形式为 headerName 。若不存在这种 Header 头,空 QByteArray 被返回,可能难以区分存在但没有内容的 Header 头 (使用 hasRawHeader () 以确定 Header 头是否存在)。
可以设置原生 Header 头采用 setRawHeader () 或采用 setHeader ().
另请参阅 header () 和 setRawHeader ().
Returns a list of all raw headers that are set in this network request. The list is in the order that the headers were set.
另请参阅 hasRawHeader () 和 rawHeader ().
Sets the attribute associated with code code to be value value . If the attribute is already set, the previous value is discarded. In special, if value 是无效 QVariant , the attribute is unset.
另请参阅 attribute () 和 QNetworkRequest::Attribute .
[since 6.2]
void
QNetworkRequest::
setDecompressedSafetyCheckThreshold
(
qint64
threshold
)
设置 threshold for archive bomb checks.
Some supported compression algorithms can, in a tiny compressed file, encode a spectacularly huge decompressed file. This is only possible if the decompressed content is extremely monotonous, which is seldom the case for real files being transmitted in good faith: files exercising such insanely high compression ratios are typically payloads of buffer-overrun attacks, or denial-of-service (by using up too much memory) attacks. Consequently, files that decompress to huge sizes, particularly from tiny compressed forms, are best rejected as suspected malware.
If a reply's decompressed size is bigger than this threshold (by default, 10 MiB, i.e. 10 * 1024 * 1024), Qt will check the compression ratio: if that is unreasonably large (40:1 for GZip and Deflate, or 100:1 for Brotli and ZStandard), the reply will be treated as an error. Setting the threshold to
-1
disables this check.
该函数在 Qt 6.2 引入。
另请参阅 decompressedSafetyCheckThreshold ().
Sets the value of the known header header 到 value ,覆盖任何先前设置的 Header 头。此操作还设置等效原生 HTTP 头。
另请参阅 KnownHeaders , setRawHeader (),和 header ().
[since 5.14]
void
QNetworkRequest::
setHttp2Configuration
(const
QHttp2Configuration
&
configuration
)
Sets request's HTTP/2 parameters from configuration .
注意: The configuration must be set prior to making a request.
注意: HTTP/2 multiplexes several streams in a single HTTP/2 connection. This implies that QNetworkAccessManager will use the configuration found in the first request from a series of requests sent to the same host.
该函数在 Qt 5.14 引入。
另请参阅 http2Configuration , QNetworkAccessManager ,和 QHttp2Configuration .
[since 5.6]
void
QNetworkRequest::
setMaximumRedirectsAllowed
(
int
maxRedirectsAllowed
)
Sets the maximum number of redirects allowed to be followed for this request to maxRedirectsAllowed .
该函数在 Qt 5.6 引入。
另请参阅 maximumRedirectsAllowed ().
Allows setting a reference to the object initiating the request.
For example Qt WebKit sets the originating object to the QWebFrame that initiated the request.
另请参阅 originatingObject ().
[since 5.13]
void
QNetworkRequest::
setPeerVerifyName
(const
QString
&
peerName
)
设置 peerName as host name for the certificate validation, instead of the one used for the TCP connection.
该函数在 Qt 5.13 引入。
另请参阅 peerVerifyName .
把此请求的优先级设为 priority .
注意: priority is only a hint to the network access manager. It can use it or not. Currently it is used for HTTP to decide which request should be sent first to a server.
另请参阅 priority ().
设置 Header 头 headerName 到值 headerValue 。若 headerName 对应已知 Header 头部 (见 QNetworkRequest::KnownHeaders ),原生格式将被剖析,相应 cooked 头也将被设置。
例如:
request.setRawHeader(QByteArray("Last-Modified"), QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"));
还将设置已知 Header LastModifiedHeader to be the QDateTime 剖析日期对象。
注意: 设置相同的头 2 次覆盖先前设置。要完成相同名称多个 HTTP 头的行为,应采用逗号 , 分隔它们并串联这 2 个值,然后设置一个原生 Header 头。
另请参阅 KnownHeaders , setHeader (), hasRawHeader (),和 rawHeader ().
Sets this network request's SSL configuration to be config . The settings that apply are the private key, the local certificate, the SSL protocol (SSLv2, SSLv3, TLSv1.0 where applicable), the CA certificates and the ciphers that the SSL backend is allowed to use.
另请参阅 sslConfiguration () 和 QSslConfiguration::defaultConfiguration ().
[since 5.15]
void
QNetworkRequest::
setTransferTimeout
(
int
timeout
= DefaultTransferTimeoutConstant)
设置 timeout 作为传输超时 (以毫秒为单位)。
Transfers are aborted if no bytes are transferred before the timeout expires. Zero means no timer is set. If no argument is provided, the timeout is QNetworkRequest::DefaultTransferTimeoutConstant . If this function is not called, the timeout is disabled and has the value zero.
该函数在 Qt 5.15 引入。
另请参阅 transferTimeout .
Sets the URL this network request is referring to be url .
另请参阅 url ().
Returns this network request's SSL configuration. By default this is the same as QSslConfiguration::defaultConfiguration ().
另请参阅 setSslConfiguration () 和 QSslConfiguration::defaultConfiguration ().
[since 5.0]
void
QNetworkRequest::
swap
(
QNetworkRequest
&
other
)
Swaps this network request with other 。此函数非常快且从不失败。
该函数在 Qt 5.0 引入。
[since 5.15]
int
QNetworkRequest::
transferTimeout
() const
返回用于传输的超时 (以毫秒为单位)。
此超时为 0 若 setTransferTimeout hasn't been called, which means that the timeout is not used.
该函数在 Qt 5.15 引入。
另请参阅 setTransferTimeout .
返回此网络请求所引用的 URL。
另请参阅 setUrl ().
返回
false
if this object is not the same as
other
.
另请参阅 operator== ().
返回
true
if this object is the same as
other
(i.e., if they have the same URL, same headers and same meta-data settings).
另请参阅 operator!= ().