QHttpHeaders Class

QHttpHeaders 是一个用于保存 HTTP 头信息的类。更多

头信息 #include <QHttpHeaders>
CMake.QHttpHeaders find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
Qt 6.7

公共类型

enum class WellKnownHeader { AIM, Accept, AcceptAdditions, AcceptCH, AcceptDatetime, …, ProtocolQuery }

公共函数

QHttpHeaders()
QHttpHeaders(const QHttpHeaders &other)
QHttpHeaders(QHttpHeaders &&other)
~QHttpHeaders()
bool append(QAnyStringView name, QAnyStringView value)
bool append(QHttpHeaders::WellKnownHeader name, QAnyStringView value)
void clear()
QByteArray combinedValue(QAnyStringView name) const
QByteArray combinedValue(QHttpHeaders::WellKnownHeader name) const
bool contains(QAnyStringView name) const
bool contains(QHttpHeaders::WellKnownHeader name) const
bool insert(qsizetype i, QAnyStringView name, QAnyStringView value)
bool insert(qsizetype i, QHttpHeaders::WellKnownHeader name, QAnyStringView value)
bool isEmpty() const
QLatin1StringView nameAt(qsizetype i) const
void removeAll(QAnyStringView name)
void removeAll(QHttpHeaders::WellKnownHeader name)
void removeAt(qsizetype i)
bool replace(qsizetype i, QAnyStringView name, QAnyStringView newValue)
bool replace(qsizetype i, QHttpHeaders::WellKnownHeader name, QAnyStringView newValue)
(since 6.8) bool replaceOrAppend(QHttpHeaders::WellKnownHeader name, QAnyStringView newValue)
bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue)
void reserve(qsizetype size)
qsizetype size() const
void swap(QHttpHeaders &other)
QList<std::pair<QByteArray, QByteArray>> toListOfPairs() const
QMultiHash<QByteArray, QByteArray> toMultiHash() const
QMultiMap<QByteArray, QByteArray> toMultiMap() const
QByteArrayView value(QAnyStringView name, QByteArrayView defaultValue = {}) const
QByteArrayView value(QHttpHeaders::WellKnownHeader name, QByteArrayView defaultValue = {}) const
QByteArrayView valueAt(qsizetype i) const
QList<QByteArray> values(QAnyStringView name) const
QList<QByteArray> values(QHttpHeaders::WellKnownHeader name) const
QHttpHeaders &operator=(QHttpHeaders &&other)
QHttpHeaders &operator=(const QHttpHeaders &other)

静态公共成员

QHttpHeaders fromListOfPairs(const QList<std::pair<QByteArray, QByteArray>> &headers)
QHttpHeaders fromMultiHash(const QMultiHash<QByteArray, QByteArray> &headers)
QHttpHeaders fromMultiMap(const QMultiMap<QByteArray, QByteArray> &headers)
QByteArrayView wellKnownHeaderName(QHttpHeaders::WellKnownHeader name)
QDebug operator<<(QDebug debug, const QHttpHeaders &headers)

详细说明

该类是一个接口类型,用于使用或消费此类标头的 Qt Network API。

允许的字段名称和值字符

HTTP 头信息由名称组成。设置时,QHttpHeaders 会验证名称是否只包含 HTTP RFC 允许的字符。详细信息请参见RFC 9110 第 5.1 和 5.5 章

总之,这意味着

  • name 必须由可见的 ASCII 字符组成,且不得为空
  • value value 可以为空。

该类的设置器会自动从值中移除任何前导空格或尾部空格,因为在处理过程中必须忽略它们。

组合值

大多数 HTTP 标头值都可以用一个逗号',' 加上一个可选的空格来组合,而且语义都会得到保留。例如,这两个值在语义上应该是相似的:

// Values as separate header entries
myheadername: myheadervalue1
myheadername: myheadervalue2
// Combined value
myheadername: myheadervalue1, myheadervalue2

不过,这条规则有一个明显的例外:Set-Cookie。因此,QHttpHeaders 不会自动合并这些值。

性能

大多数 QHttpHeaders 函数都提供QHttpHeaders::WellKnownHeaderQAnyStringView 两种重载。从内存使用和计算的角度来看,建议使用QHttpHeaders::WellKnownHeader 重载。

成员类型文档

enum class QHttpHeaders::WellKnownHeader

根据IANA 注册表列出的知名标头。

常数
QHttpHeaders::WellKnownHeader::AIM0
QHttpHeaders::WellKnownHeader::Accept1
QHttpHeaders::WellKnownHeader::AcceptAdditions2
QHttpHeaders::WellKnownHeader::AcceptCH3
QHttpHeaders::WellKnownHeader::AcceptDatetime4
QHttpHeaders::WellKnownHeader::AcceptEncoding5
QHttpHeaders::WellKnownHeader::AcceptFeatures6
QHttpHeaders::WellKnownHeader::AcceptLanguage7
QHttpHeaders::WellKnownHeader::AcceptPatch8
QHttpHeaders::WellKnownHeader::AcceptPost9
QHttpHeaders::WellKnownHeader::AcceptRanges10
QHttpHeaders::WellKnownHeader::AcceptSignature11
QHttpHeaders::WellKnownHeader::AccessControlAllowCredentials12
QHttpHeaders::WellKnownHeader::AccessControlAllowHeaders13
QHttpHeaders::WellKnownHeader::AccessControlAllowMethods14
QHttpHeaders::WellKnownHeader::AccessControlAllowOrigin15
QHttpHeaders::WellKnownHeader::AccessControlExposeHeaders16
QHttpHeaders::WellKnownHeader::AccessControlMaxAge17
QHttpHeaders::WellKnownHeader::AccessControlRequestHeaders18
QHttpHeaders::WellKnownHeader::AccessControlRequestMethod19
QHttpHeaders::WellKnownHeader::Age20
QHttpHeaders::WellKnownHeader::Allow21
QHttpHeaders::WellKnownHeader::ALPN22
QHttpHeaders::WellKnownHeader::AltSvc23
QHttpHeaders::WellKnownHeader::AltUsed24
QHttpHeaders::WellKnownHeader::Alternates25
QHttpHeaders::WellKnownHeader::ApplyToRedirectRef26
QHttpHeaders::WellKnownHeader::AuthenticationControl27
QHttpHeaders::WellKnownHeader::AuthenticationInfo28
QHttpHeaders::WellKnownHeader::Authorization29
QHttpHeaders::WellKnownHeader::CacheControl30
QHttpHeaders::WellKnownHeader::CacheStatus31
QHttpHeaders::WellKnownHeader::CalManagedID32
QHttpHeaders::WellKnownHeader::CalDAVTimezones33
QHttpHeaders::WellKnownHeader::CapsuleProtocol34
QHttpHeaders::WellKnownHeader::CDNCacheControl35
QHttpHeaders::WellKnownHeader::CDNLoop36
QHttpHeaders::WellKnownHeader::CertNotAfter37
QHttpHeaders::WellKnownHeader::CertNotBefore38
QHttpHeaders::WellKnownHeader::ClearSiteData39
QHttpHeaders::WellKnownHeader::ClientCert40
QHttpHeaders::WellKnownHeader::ClientCertChain41
QHttpHeaders::WellKnownHeader::Close42
QHttpHeaders::WellKnownHeader::Connection43
QHttpHeaders::WellKnownHeader::ContentDigest44
QHttpHeaders::WellKnownHeader::ContentDisposition45
QHttpHeaders::WellKnownHeader::ContentEncoding46
QHttpHeaders::WellKnownHeader::ContentID47
QHttpHeaders::WellKnownHeader::ContentLanguage48
QHttpHeaders::WellKnownHeader::ContentLength49
QHttpHeaders::WellKnownHeader::ContentLocation50
QHttpHeaders::WellKnownHeader::ContentRange51
QHttpHeaders::WellKnownHeader::ContentSecurityPolicy52
QHttpHeaders::WellKnownHeader::ContentSecurityPolicyReportOnly53
QHttpHeaders::WellKnownHeader::ContentType54
QHttpHeaders::WellKnownHeader::Cookie55
QHttpHeaders::WellKnownHeader::CrossOriginEmbedderPolicy56
QHttpHeaders::WellKnownHeader::CrossOriginEmbedderPolicyReportOnly57
QHttpHeaders::WellKnownHeader::CrossOriginOpenerPolicy58
QHttpHeaders::WellKnownHeader::CrossOriginOpenerPolicyReportOnly59
QHttpHeaders::WellKnownHeader::CrossOriginResourcePolicy60
QHttpHeaders::WellKnownHeader::DASL61
QHttpHeaders::WellKnownHeader::Date62
QHttpHeaders::WellKnownHeader::DAV63
QHttpHeaders::WellKnownHeader::DeltaBase64
QHttpHeaders::WellKnownHeader::Depth65
QHttpHeaders::WellKnownHeader::Destination66
QHttpHeaders::WellKnownHeader::DifferentialID67
QHttpHeaders::WellKnownHeader::DPoP68
QHttpHeaders::WellKnownHeader::DPoPNonce69
QHttpHeaders::WellKnownHeader::EarlyData70
QHttpHeaders::WellKnownHeader::ETag71
QHttpHeaders::WellKnownHeader::Expect72
QHttpHeaders::WellKnownHeader::ExpectCT73
QHttpHeaders::WellKnownHeader::Expires74
QHttpHeaders::WellKnownHeader::Forwarded75
QHttpHeaders::WellKnownHeader::From76
QHttpHeaders::WellKnownHeader::Hobareg77
QHttpHeaders::WellKnownHeader::Host78
QHttpHeaders::WellKnownHeader::If79
QHttpHeaders::WellKnownHeader::IfMatch80
QHttpHeaders::WellKnownHeader::IfModifiedSince81
QHttpHeaders::WellKnownHeader::IfNoneMatch82
QHttpHeaders::WellKnownHeader::IfRange83
QHttpHeaders::WellKnownHeader::IfScheduleTagMatch84
QHttpHeaders::WellKnownHeader::IfUnmodifiedSince85
QHttpHeaders::WellKnownHeader::IM86
QHttpHeaders::WellKnownHeader::IncludeReferredTokenBindingID87
QHttpHeaders::WellKnownHeader::KeepAlive88
QHttpHeaders::WellKnownHeader::Label89
QHttpHeaders::WellKnownHeader::LastEventID90
QHttpHeaders::WellKnownHeader::LastModified91
QHttpHeaders::WellKnownHeader::Link92
QHttpHeaders::WellKnownHeader::Location93
QHttpHeaders::WellKnownHeader::LockToken94
QHttpHeaders::WellKnownHeader::MaxForwards95
QHttpHeaders::WellKnownHeader::MementoDatetime96
QHttpHeaders::WellKnownHeader::Meter97
QHttpHeaders::WellKnownHeader::MIMEVersion98
QHttpHeaders::WellKnownHeader::Negotiate99
QHttpHeaders::WellKnownHeader::NEL100
QHttpHeaders::WellKnownHeader::ODataEntityId101
QHttpHeaders::WellKnownHeader::ODataIsolation102
QHttpHeaders::WellKnownHeader::ODataMaxVersion103
QHttpHeaders::WellKnownHeader::ODataVersion104
QHttpHeaders::WellKnownHeader::OptionalWWWAuthenticate105
QHttpHeaders::WellKnownHeader::OrderingType106
QHttpHeaders::WellKnownHeader::Origin107
QHttpHeaders::WellKnownHeader::OriginAgentCluster108
QHttpHeaders::WellKnownHeader::OSCORE109
QHttpHeaders::WellKnownHeader::OSLCCoreVersion110
QHttpHeaders::WellKnownHeader::Overwrite111
QHttpHeaders::WellKnownHeader::PingFrom112
QHttpHeaders::WellKnownHeader::PingTo113
QHttpHeaders::WellKnownHeader::Position114
QHttpHeaders::WellKnownHeader::Prefer115
QHttpHeaders::WellKnownHeader::PreferenceApplied116
QHttpHeaders::WellKnownHeader::Priority117
QHttpHeaders::WellKnownHeader::ProxyAuthenticate118
QHttpHeaders::WellKnownHeader::ProxyAuthenticationInfo119
QHttpHeaders::WellKnownHeader::ProxyAuthorization120
QHttpHeaders::WellKnownHeader::ProxyStatus121
QHttpHeaders::WellKnownHeader::PublicKeyPins122
QHttpHeaders::WellKnownHeader::PublicKeyPinsReportOnly123
QHttpHeaders::WellKnownHeader::Range124
QHttpHeaders::WellKnownHeader::RedirectRef125
QHttpHeaders::WellKnownHeader::Referer126
QHttpHeaders::WellKnownHeader::Refresh127
QHttpHeaders::WellKnownHeader::ReplayNonce128
QHttpHeaders::WellKnownHeader::ReprDigest129
QHttpHeaders::WellKnownHeader::RetryAfter130
QHttpHeaders::WellKnownHeader::ScheduleReply131
QHttpHeaders::WellKnownHeader::ScheduleTag132
QHttpHeaders::WellKnownHeader::SecPurpose133
QHttpHeaders::WellKnownHeader::SecTokenBinding134
QHttpHeaders::WellKnownHeader::SecWebSocketAccept135
QHttpHeaders::WellKnownHeader::SecWebSocketExtensions136
QHttpHeaders::WellKnownHeader::SecWebSocketKey137
QHttpHeaders::WellKnownHeader::SecWebSocketProtocol138
QHttpHeaders::WellKnownHeader::SecWebSocketVersion139
QHttpHeaders::WellKnownHeader::Server140
QHttpHeaders::WellKnownHeader::ServerTiming141
QHttpHeaders::WellKnownHeader::SetCookie142
QHttpHeaders::WellKnownHeader::Signature143
QHttpHeaders::WellKnownHeader::SignatureInput144
QHttpHeaders::WellKnownHeader::SLUG145
QHttpHeaders::WellKnownHeader::SoapAction146
QHttpHeaders::WellKnownHeader::StatusURI147
QHttpHeaders::WellKnownHeader::StrictTransportSecurity148
QHttpHeaders::WellKnownHeader::Sunset149
QHttpHeaders::WellKnownHeader::SurrogateCapability150
QHttpHeaders::WellKnownHeader::SurrogateControl151
QHttpHeaders::WellKnownHeader::TCN152
QHttpHeaders::WellKnownHeader::TE153
QHttpHeaders::WellKnownHeader::Timeout154
QHttpHeaders::WellKnownHeader::Topic155
QHttpHeaders::WellKnownHeader::Traceparent156
QHttpHeaders::WellKnownHeader::Tracestate157
QHttpHeaders::WellKnownHeader::Trailer158
QHttpHeaders::WellKnownHeader::TransferEncoding159
QHttpHeaders::WellKnownHeader::TTL160
QHttpHeaders::WellKnownHeader::Upgrade161
QHttpHeaders::WellKnownHeader::Urgency162
QHttpHeaders::WellKnownHeader::UserAgent163
QHttpHeaders::WellKnownHeader::VariantVary164
QHttpHeaders::WellKnownHeader::Vary165
QHttpHeaders::WellKnownHeader::Via166
QHttpHeaders::WellKnownHeader::WantContentDigest167
QHttpHeaders::WellKnownHeader::WantReprDigest168
QHttpHeaders::WellKnownHeader::WWWAuthenticate169
QHttpHeaders::WellKnownHeader::XContentTypeOptions170
QHttpHeaders::WellKnownHeader::XFrameOptions171
QHttpHeaders::WellKnownHeader::AcceptCharset172
QHttpHeaders::WellKnownHeader::CPEPInfo173
QHttpHeaders::WellKnownHeader::Pragma174
QHttpHeaders::WellKnownHeader::ProtocolInfo175
QHttpHeaders::WellKnownHeader::ProtocolQuery176

成员函数文档

[noexcept] QHttpHeaders::QHttpHeaders()

创建一个新的 QHttpHeaders 对象。

QHttpHeaders::QHttpHeaders(const QHttpHeaders &other)

创建other 的副本。

[noexcept] QHttpHeaders::QHttpHeaders(QHttpHeaders &&other)

移动-从other 开始构建对象,该对象将离开empty

[noexcept] QHttpHeaders::~QHttpHeaders()

处理标头对象。

bool QHttpHeaders::append(QAnyStringView name, QAnyStringView value)

namevalue 追加头条目,如果成功则返回true

另请参阅 append(QHttpHeaders::WellKnownHeader, QAnyStringView) 和Allowed field name and value characters

bool QHttpHeaders::append(QHttpHeaders::WellKnownHeader name, QAnyStringView value)

该函数重载了append(QAnyStringView, QAnyStringView)。

void QHttpHeaders::clear()

清除所有标头条目。

另请参阅 size().

QByteArray QHttpHeaders::combinedValue(QAnyStringView name) const

以逗号连接的字符串形式返回name 头的值。如果带有name 的标头不存在,则返回null QByteArray

注意: 以这种方式访问 "Set-Cookie "标头的值可能无法达到预期效果。在HTTP RFC中,这是一个明显的例外,因为它的值不能以这种方式组合。请使用values() 代替。

另请参阅 values(QAnyStringView)。

QByteArray QHttpHeaders::combinedValue(QHttpHeaders::WellKnownHeader name) const

该函数重载了combinedValue(QAnyStringView)。

bool QHttpHeaders::contains(QAnyStringView name) const

返回头信息是否包含带有name 的头信息。

另请参阅 contains(QHttpHeaders::WellKnownHeader)。

bool QHttpHeaders::contains(QHttpHeaders::WellKnownHeader name) const

该函数重载了 has(QAnyStringView)。

[static] QHttpHeaders QHttpHeaders::fromListOfPairs(const QList<std::pair<QByteArray, QByteArray>> &headers)

创建一个新的QHttpHeaders 对象,该对象由headers 填充。

另请参阅 Allowed field name and value characters

[static] QHttpHeaders QHttpHeaders::fromMultiHash(const QMultiHash<QByteArray, QByteArray> &headers)

创建一个新的QHttpHeaders 对象,该对象由headers 填充。

另请参阅 Allowed field name and value characters

[static] QHttpHeaders QHttpHeaders::fromMultiMap(const QMultiMap<QByteArray, QByteArray> &headers)

创建一个新的QHttpHeaders 对象,该对象由headers 填充。

另请参阅 Allowed field name and value characters

bool QHttpHeaders::insert(qsizetype i, QAnyStringView name, QAnyStringView value)

在索引i 处插入一个头文件条目,其中包含namevalue 。索引必须有效(参见size() )。返回插入是否成功。

另请参阅 append(),insert(qsizetype, QHttpHeaders::WellKnownHeader, QAnyStringView),size() 和Allowed field name and value characters

bool QHttpHeaders::insert(qsizetype i, QHttpHeaders::WellKnownHeader name, QAnyStringView value)

该函数重载了insert(qsizetype, QAnyStringView, QAnyStringView)。

[noexcept] bool QHttpHeaders::isEmpty() const

如果标头大小为 0,则返回true ;否则返回false

另请参阅 size() 。

[noexcept] QLatin1StringView QHttpHeaders::nameAt(qsizetype i) const

返回索引i 中的标头名称。索引i 必须有效(参见size() )。

标头名称不区分大小写,返回的名称小写。

另请参阅 size() 和valueAt()。

void QHttpHeaders::removeAll(QAnyStringView name)

移除头name

另请参阅 removeAt() 和removeAll(QHttpHeaders::WellKnownHeader)。

void QHttpHeaders::removeAll(QHttpHeaders::WellKnownHeader name)

该函数重载了removeAll(QAnyStringView)。

void QHttpHeaders::removeAt(qsizetype i)

删除索引i 上的标头。索引i 必须有效(参见size()).

另请参阅 removeAll(QHttpHeaders::WellKnownHeader)、removeAll(QAnyStringView) 和size() 。

bool QHttpHeaders::replace(qsizetype i, QAnyStringView name, QAnyStringView newValue)

namenewValue 替换索引i 中的页眉条目。索引必须有效(参见size() )。返回替换是否成功。

另请参阅 append(),replace(qsizetype, QHttpHeaders::WellKnownHeader, QAnyStringView),size() 和Allowed field name and value characters

bool QHttpHeaders::replace(qsizetype i, QHttpHeaders::WellKnownHeader name, QAnyStringView newValue)

该函数重载了replace(qsizetype, QAnyStringView, QAnyStringView)。

[since 6.8] bool QHttpHeaders::replaceOrAppend(QHttpHeaders::WellKnownHeader name, QAnyStringView newValue)

如果QHttpHeaders 已包含name ,则将其值替换为newValue ,并删除可能的其他name 条目。如果name 不存在,则添加一个新条目。如果成功,则返回true

该函数是一种方便的方法,用于设置唯一的name:newValue 标头。对于大多数标头来说,相对顺序并不重要,因此如果存在现有条目,就可以重复使用。

此函数在 Qt 6.8 中引入。

另请参阅 replaceOrAppend(QAnyStringView, QAnyStringView)。

bool QHttpHeaders::replaceOrAppend(QAnyStringView name, QAnyStringView newValue)

此函数重载 replaceOrAppend(WellKnownHeader, QAnyStringView)。

void QHttpHeaders::reserve(qsizetype size)

尝试为至少size 头文件分配内存。

如果事先知道将有多少个报头条目,则可以调用此函数来防止重新分配和内存碎片。

[noexcept] qsizetype QHttpHeaders::size() const

返回页眉条目的数量。

[noexcept] void QHttpHeaders::swap(QHttpHeaders &other)

QHttpHeadersother 互换。这一操作非常快速,而且从未出现过故障。

QList<std::pair<QByteArray, QByteArray>> QHttpHeaders::toListOfPairs() const

以(名称、值)对列表的形式返回标头条目。标头名称不区分大小写,返回的名称小写。

QMultiHash<QByteArray, QByteArray> QHttpHeaders::toMultiHash() const

返回从名称到值的哈希值。标头名称不区分大小写,返回的名称小写。

QMultiMap<QByteArray, QByteArray> QHttpHeaders::toMultiMap() const

以从名称到值的映射形式返回标头条目。标头名称不区分大小写,返回的名称小写。

[noexcept] QByteArrayView QHttpHeaders::value(QAnyStringView name, QByteArrayView defaultValue = {}) const

返回(第一个)头name 的值,如果不存在,则返回defaultValue

另请参阅 value(QHttpHeaders::WellKnownHeader, QByteArrayView)。

[noexcept] QByteArrayView QHttpHeaders::value(QHttpHeaders::WellKnownHeader name, QByteArrayView defaultValue = {}) const

该函数重载了value (QAnyStringView、QByteArrayView)。

[noexcept] QByteArrayView QHttpHeaders::valueAt(qsizetype i) const

返回索引i 处的标头值。索引i 必须有效(参见size() )。

另请参阅 size()、value()、values()、combinedValue() 和nameAt()。

QList<QByteArray> QHttpHeaders::values(QAnyStringView name) const

以列表形式返回页眉name 的值。如果不存在name 的头信息,则返回空列表。

另请参阅 values(QHttpHeaders::WellKnownHeader)。

QList<QByteArray> QHttpHeaders::values(QHttpHeaders::WellKnownHeader name) const

该函数重载了values(QAnyStringView)。

[static noexcept] QByteArrayView QHttpHeaders::wellKnownHeaderName(QHttpHeaders::WellKnownHeader name)

返回与作为视图提供的name 相对应的页眉名称。

[noexcept] QHttpHeaders &QHttpHeaders::operator=(QHttpHeaders &&other)

Move-assignsother 并返回对该对象的引用。

other 将离开 。empty

QHttpHeaders &QHttpHeaders::operator=(const QHttpHeaders &other)

指定other 的内容,并返回对该对象的引用。

相关非成员

QDebug operator<<(QDebug debug, const QHttpHeaders &headers)

headers 写入debug 数据流。

© 2025 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 the GNU 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.