0


Roson的Qt之旅 #132 QNetworkRequest(网络请求)

CSDN话题挑战赛第2期
参赛话题:Qt应用程序开发

1.详细描述

QNetworkRequest类持有一个要用QNetworkAccessManager发送的请求。 QNetworkRequest是网络访问API的一部分,是持有通过网络发送请求所需信息的类。它包含一个URL和一些辅助信息,可以用来修改请求。 也请参见QNetworkReply和QNetworkAccessManager。

2.成员类型说明

enum QNetworkRequest::Attribute

用于QNetworkRequest和QNetworkReply的属性代码。 属性是额外的元数据,用于控制请求的行为,并从回复中传递进一步的信息给应用程序。属性也是可扩展的,允许自定义实现传递自定义值。 下表解释了默认的属性代码是什么,与之相关的QVariant类型,如果所述属性缺失的默认值,以及它是在请求还是回复中使用。
ConstantValueDescriptionQNetworkRequest::HttpStatusCodeAttribute0Replies only, type: 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::HttpReasonPhraseAttribute1Replies only, type: 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::RedirectionTargetAttribute2Replies only, type: QMetaType::QUrl (no default) If present, it indicates that the server is redirecting the request to a different URL. The Network Access API does not by default follow redirections: the application can determine if the requested redirection should be allowed, according to its security policies, or it can set QNetworkRequest::FollowRedirectsAttribute to true (in which case the redirection will be followed and this attribute will not be present in the reply). The returned URL might be relative. Use QUrl::resolved() to create an absolute URL out of it.QNetworkRequest::ConnectionEncryptedAttribute3Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained through an encrypted (secure) connection.QNetworkRequest::CacheLoadControlAttribute4Requests only, type: 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::CacheSaveControlAttribute5Requests only, type: 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::SourceIsFromCacheAttribute6Replies only, type: QMetaType::Bool (default: false) Indicates whether the data was obtained from cache or not.QNetworkRequest::DoNotBufferUploadDataAttribute7Requests only, type: 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::HttpPipeliningAllowedAttribute8Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP pipelining with this request.QNetworkRequest::HttpPipeliningWasUsedAttribute9Replies only, type: QMetaType::Bool Indicates whether the HTTP pipelining was used for receiving this reply.QNetworkRequest::CustomVerbAttribute10Requests only, type: 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::CookieLoadControlAttribute11Requests only, type: 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::CookieSaveControlAttribute13Requests only, type: 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::AuthenticationReuseAttribute12Requests only, type: 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::BackgroundRequestAttribute17Type: 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. The QNetworkSession ConnectInBackground property will be set according to this attribute.QNetworkRequest::SpdyAllowedAttribute18Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use SPDY with this request. This applies only to SSL requests, and depends on the server supporting SPDY.QNetworkRequest::SpdyWasUsedAttribute19Replies only, type: QMetaType::Bool Indicates whether SPDY was used for receiving this reply.QNetworkRequest::HTTP2AllowedAttribute22Requests only, type: QMetaType::Bool (default: false) Indicates whether the QNetworkAccessManager code is allowed to use HTTP/2 with this request. This applies to SSL requests or 'cleartext' HTTP/2.QNetworkRequest::HTTP2WasUsedAttribute23Replies only, type: QMetaType::Bool (default: false) Indicates whether HTTP/2 was used for receiving this reply. (This value was introduced in 5.9.)QNetworkRequest::EmitAllUploadProgressSignalsAttribute20Requests only, type: 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::FollowRedirectsAttribute21Requests only, type: QMetaType::Bool (default: false) Indicates whether the Network Access API should automatically follow a HTTP redirect response or not. Currently redirects that are insecure, that is redirecting from "https" to "http" protocol, are not allowed. (This value was introduced in 5.6.)QNetworkRequest::OriginalContentLengthAttribute24Replies only, type 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::RedirectPolicyAttribute25Requests only, type: QMetaType::Int, should be one of the QNetworkRequest::RedirectPolicy values (default: ManualRedirectPolicy). This attribute obsoletes FollowRedirectsAttribute. (This value was introduced in 5.9.)QNetworkRequest::User1000Special 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::UserMax32767Special type. See User.
enum QNetworkRequest::CacheLoadControl

控制QNetworkAccessManager的缓存机制。
ConstantValueDescriptionQNetworkRequest::AlwaysNetwork0always 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::PreferNetwork1default 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::PreferCache2load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache.QNetworkRequest::AlwaysCache3only load from cache, indicating error if the item was not cached (i.e., off-line mode)
enum QNetworkRequest::KnownHeaders

QNetworkRequest所解析的已知头类型的列表。每个已知的头也以原始形式表示,并附有完整的HTTP名称。
ConstantValueDescriptionQNetworkRequest::ContentDispositionHeader6Corresponds to the HTTP Content-Disposition header and contains a string containing the disposition type (for instance, attachment) and a parameter (for instance, filename).QNetworkRequest::ContentTypeHeader0Corresponds to the HTTP Content-Type header and contains a string containing the media (MIME) type and any auxiliary data (for instance, charset).QNetworkRequest::ContentLengthHeader1Corresponds to the HTTP Content-Length header and contains the length in bytes of the data transmitted.QNetworkRequest::LocationHeader2Corresponds to the HTTP Location header and contains a URL representing the actual location of the data, including the destination URL in case of redirections.QNetworkRequest::LastModifiedHeader3Corresponds to the HTTP Last-Modified header and contains a QDateTime representing the last modification date of the contents.QNetworkRequest::CookieHeader4Corresponds to the HTTP Cookie header and contains a QList<QNetworkCookie> representing the cookies to be sent back to the server.QNetworkRequest::SetCookieHeader5Corresponds to the HTTP Set-Cookie header and contains a QList<QNetworkCookie> representing the cookies sent by the server to be stored locally.QNetworkRequest::UserAgentHeader7The User-Agent header sent by HTTP clients.QNetworkRequest::ServerHeader8The Server header received by HTTP clients.
参见 header(), setHeader(), rawHeader(), 和setRawHeader().

enum QNetworkRequest::LoadControl

表示请求的加载机制的某个方面是否被手动重写,例如被Qt WebKit重写。
ConstantValueDescriptionQNetworkRequest::Automatic0default value: indicates default behaviour.QNetworkRequest::Manual1indicates behaviour has been manually overridden.
enum QNetworkRequest::Priority

该枚举列出了可能的网络请求优先级。
ConstantValueDescriptionQNetworkRequest::HighPriority1High priorityQNetworkRequest::NormalPriority3Normal priorityQNetworkRequest::LowPriority5Low priority
enum QNetworkRequest::RedirectPolicy

表示网络访问API是否应该自动跟踪HTTP重定向响应。
ConstantValueDescriptionQNetworkRequest::ManualRedirectPolicy0Default value: not following any redirects.QNetworkRequest::NoLessSafeRedirectPolicy1Only "http"->"http", "http" -> "https" or "https" -> "https" redirects are allowed. Equivalent to setting the old FollowRedirectsAttribute to trueQNetworkRequest::SameOriginRedirectPolicy2Require the same protocol, host and port. Note, Example Domain and Example Domain will fail with this policy (implicit/explicit ports are considered to be a mismatch).QNetworkRequest::UserVerifiedRedirectPolicy3Client decides whether to follow each redirect by handling the redirected() signal, emitting redirectAllowed() on the QNetworkReply object to allow the redirect or aborting/finishing it to reject the redirect. This can be used, for example, to ask the user whether to accept the redirect, or to decide based on some app-specific configuration.

3.成员函数说明

QNetworkRequest::QNetworkRequest(const QUrl &url = QUrl())

构造一个QNetworkRequest对象,将url作为被请求的URL。 参见url()和setUrl()。

QNetworkRequest::QNetworkRequest(const QNetworkRequest &other)

创建一个other的副本。

QNetworkRequest::~QNetworkRequest()

析构函数

QVariant QNetworkRequest::attribute(Attribute code, const QVariant &defaultValue = QVariant()) const

返回与代码编码相关的属性。如果该属性没有被设置,它将返回defaultValue。 注意:这个函数不应用QNetworkRequest::Attribute中列出的默认值。 另请参见setAttribute()和QNetworkRequest::Attribute。

bool QNetworkRequest::hasRawHeader(const QByteArray &headerName) const

如果这个网络请求中存在原始头信息headerName,则返回true。 参见rawHeader()和setRawHeader()。

QVariant QNetworkRequest::header(KnownHeaders header) const

如果此请求中存在已知的网络头文件,则返回该文件的值。如果它不存在,则返回QVariant()(即,一个无效的变量)。 参见KnownHeaders, rawHeader(), and setHeader()。

int QNetworkRequest::maximumRedirectsAllowed() const

返回这个请求允许遵循的最大重定向数量。 这个函数在Qt 5.6中引入。 也请参见setMaximumRedirectsAllowed()。

*QObject QNetworkRequest::originatingObject() const

返回对发起这个网络请求的对象的引用;如果没有设置或该对象已被销毁,则返回0。 这个函数在Qt 4.6中引入。 也请参见setOriginatingObject()。

Priority QNetworkRequest::priority() const

返回这个请求的优先级。 这个函数在Qt 4.7中被引入。 也见setPriority()。

QByteArray QNetworkRequest::rawHeader(const QByteArray &headerName) const

返回头信息headerName的原始形式。如果没有这样的头,将返回一个空的QByteArray,这可能与一个存在但没有内容的头无法区分(使用hasRawHeader()来找出头是否存在)。 原始头可以用setRawHeader()或setHeader()来设置。 参见header()和setRawHeader()。

QList<QByteArray> QNetworkRequest::rawHeaderList() const

返回该网络请求中设置的所有原始头信息的列表。该列表是按照头信息被设置的顺序排列的。 参见hasRawHeader()和rawHeader()。

void QNetworkRequest::setAttribute(Attribute code, const QVariant &value)

将与code代码相关的属性设置为value值。如果该属性已经被设置,那么之前的值将被丢弃。在特殊情况下,如果value是一个无效的QVariant,该属性将被取消设置。 参见 attribute() 和 QNetworkRequest::Attribute。

void QNetworkRequest::setHeader(KnownHeaders header, const QVariant &value)

将已知头信息的值设置为value,覆盖之前设置的任何头信息。这个操作也会设置相应的原始HTTP头。 参见KnownHeaders, setRawHeader(), and header().

void QNetworkRequest::setMaximumRedirectsAllowed(int maxRedirectsAllowed)

将此请求允许遵循的最大重定向数量设置为maxRedirectsAllowed。 这个函数在Qt 5.6中引入。 也请参见maximumRedirectsAllowed()。

*void QNetworkRequest::setOriginatingObject(QObject object)

允许设置一个对发起请求的对象的引用。 例如,Qt WebKit将始发对象设置为发起请求的QWebFrame。 这个函数在Qt 4.6中被引入。 参见originatingObject()。

void QNetworkRequest::setPriority(Priority priority)

将此请求的优先级设置为优先级。 注意:优先级只是对网络访问管理器的一个提示。它可以使用它,也可以不使用。目前它被用于HTTP,以决定哪个请求应该被首先发送到服务器上。 这个函数是在Qt 4.7中引入的。 也请看优先级()。

void QNetworkRequest::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)

将头信息headerName设置为headerValue的值。如果headerName对应于一个已知的头(见QNetworkRequest::KnownHeaders),原始格式将被解析,相应的 "熟 "头也将被设置。 比如说:

  request.setRawHeader(QByteArray("Last-Modified"), QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"));
也会将已知的头LastModifiedHeader设置为解析后的日期的QDateTime对象。 注意:两次设置相同的头信息会覆盖之前的设置。为了完成多个同名HTTP头的行为,你应该把两个值连接起来,用逗号(",")隔开,然后设置一个单一的原始头信息。 参见KnownHeaders, setHeader(), hasRawHeader(), and rawHeader()。

void QNetworkRequest::setSslConfiguration(const QSslConfiguration &config)

设置此网络请求的SSL配置为config。适用的设置是私钥、本地证书、SSL协议(SSLv2、SSLv3、TLSv1.0,如适用)、CA证书和SSL后端允许使用的密码。 默认情况下,没有设置SSL配置,这使得后端可以自由选择最适合他们的配置。 参见sslConfiguration()和QSslConfiguration::defaultConfiguration()。

void QNetworkRequest::setUrl(const QUrl &url)

将此网络请求所指的URL设置为url。 也请参见url()。

QSslConfiguration QNetworkRequest::sslConfiguration() const

返回这个网络请求的SSL配置。默认情况下,没有指定SSL设置。 参见setSslConfiguration()。

void QNetworkRequest::swap(QNetworkRequest &other)

将此网络请求与其他网络请求交换。这个函数非常快,而且从不失败。 这个函数在Qt 5.0中引入。

QUrl QNetworkRequest::url() const

返回这个网络请求所指向的URL。 参见setUrl()。

标签: qt ui QNetworkRequest

本文转载自: https://blog.csdn.net/jolin678/article/details/127173853
版权归原作者 Allen Roson 所有, 如有侵权,请联系我们删除。

“Roson的Qt之旅 #132 QNetworkRequest(网络请求)”的评论:

还没有评论