本页内容

QHttpHeaders Class

QHttpHeaders 是一个用于存储 HTTP 头信息的类。更多内容...

头文件: #include <QHttpHeaders>
CMake: 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
(since 6.10) std::optional<QDateTime> dateTimeValue(QAnyStringView name) const
(since 6.10) std::optional<QDateTime> dateTimeValue(QHttpHeaders::WellKnownHeader name) const
(since 6.10) std::optional<QDateTime> dateTimeValueAt(qsizetype i) const
(since 6.10) std::optional<QList<QDateTime>> dateTimeValues(QAnyStringView name) const
(since 6.10) std::optional<QList<QDateTime>> dateTimeValues(QHttpHeaders::WellKnownHeader name) const
bool insert(qsizetype i, QAnyStringView name, QAnyStringView value)
bool insert(qsizetype i, QHttpHeaders::WellKnownHeader name, QAnyStringView value)
(since 6.10) std::optional<qint64> intValue(QAnyStringView name) const
(since 6.10) std::optional<qint64> intValue(QHttpHeaders::WellKnownHeader name) const
(since 6.10) std::optional<qint64> intValueAt(qsizetype i) const
(since 6.10) std::optional<QList<qint64>> intValues(QAnyStringView name) const
(since 6.10) std::optional<QList<qint64>> intValues(QHttpHeaders::WellKnownHeader name) const
bool isEmpty() const
QLatin1StringView nameAt(qsizetype i) const
(since 6.12) std::optional<QHttpHeaderRangeSet> rangeValues() 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)
(since 6.10) void setDateTimeValue(QAnyStringView name, const QDateTime &dateTime)
(since 6.10) void setDateTimeValue(QHttpHeaders::WellKnownHeader name, const QDateTime &dateTime)
(since 6.12) void setRangeValues(const QHttpHeaderRangeSet &ranges)
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)
(since 6.12) QDataStream &operator<<(QDataStream &out, const QHttpHeaders &headers)
QDebug operator<<(QDebug debug, const QHttpHeaders &headers)
(since 6.12) QDataStream &operator>>(QDataStream &in, QHttpHeaders &headers)

详细说明

该类是用于使用或调用此类头文件的 Qt Network API 的接口类型。

允许的字段名称和值字符

HTTP 头由名称和 值组成。在设置这些内容时,QHttpHeaders 会验证名称和 值是否仅包含 HTTP RFC 允许的字符。有关详细信息,请参阅RFC 9110 的第 5.1 章和第 5.5 章。

总而言之,这意味着:

  • name 必须由可见的 ASCII 字符组成,且不能为空
  • value 可以由任意字节组成,只要遵循标头和用例特定的编码规则即可。value 可以为空

该类的设置器会自动从value 中移除任何前导或尾随的空格,因为在处理value时必须忽略这些空格。

值的组合

大多数 HTTP 标头值可以通过一个逗号',' 加上一个可选的空白进行组合,同时保持语义含义不变。例如,以下两行在语义上应是相似的:

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

然而,此规则有一个值得注意的例外:Set-Cookie。鉴于此情况以及可能存在的自定义使用场景,QHttpHeaders 不会自动合并这些值。

性能

大多数 QHttpHeaders 函数都提供了QHttpHeaders::WellKnownHeader 和QAnyStringView 两种重载形式。从内存使用和计算效率的角度来看,建议使用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 的副本。

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

从 `other` 移动构造该对象,该对象将被保留为 `empty`。

[noexcept] QHttpHeaders::~QHttpHeaders()

销毁 headers 对象。

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

使用name 和value 追加一个标头条目,成功时返回true 。

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

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

该函数重载了QHttpHeaders::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

该函数重载了QHttpHeaders::combinedValue (QAnyStringView)方法。

bool QHttpHeaders::contains(QAnyStringView name) const

返回头部是否包含name 的头部。

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

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

该函数重载了 QHttpHeaders::has(QAnyStringView)。

[since 6.10] std::optional<QDateTime> QHttpHeaders::dateTimeValue(QAnyStringView name) const

将name 中找到的第一个标头值转换为QDateTime 对象,遵循标准的HTTP日期格式。如果该标头不存在或包含无效的QDateTime ,则返回std::nullopt 。

该函数在 Qt 6.10 中引入。

另请参见 setDateTimeValue()、dateTimeValues(QAnyStringView name) 和dateTimeValueAt(qsizetype i)。

[since 6.10] std::optional<QDateTime> QHttpHeaders::dateTimeValue(QHttpHeaders::WellKnownHeader name) const

该函数重载了QHttpHeaders::dateTimeValue (QAnyStringView)。

该函数于 Qt 6.10 中引入。

[since 6.10] std::optional<QDateTime> QHttpHeaders::dateTimeValueAt(qsizetype i) const

将索引为i 的头部值转换为符合标准HTTP日期格式的QDateTime 对象。索引i 必须有效。

该函数于 Qt 6.10 中引入。

另请参阅 dateTimeValue(QAnyStringView name) 和dateTimeValues(QAnyStringView name)。

[since 6.10] std::optional<QList<QDateTime>> QHttpHeaders::dateTimeValues(QAnyStringView name) const

返回name 的所有标头值,将其作为QDateTime 对象的列表,并遵循标准HTTP日期格式。如果未找到有效的日期时间值,则返回std::nullopt 。

该函数在 Qt 6.10 中引入。

另请参阅 dateTimeValue(QAnyStringView name) 和dateTimeValueAt(qsizetype i)。

[since 6.10] std::optional<QList<QDateTime>> QHttpHeaders::dateTimeValues(QHttpHeaders::WellKnownHeader name) const

该函数重载了QHttpHeaders::dateTimeValues (QAnyStringView)。

该函数于 Qt 6.10 中引入。

[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 处插入一个标头条目,其值为name 和value 。该索引必须有效(参见size())。返回插入是否成功。

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

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

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

[noexcept, since 6.10] std::optional<qint64> QHttpHeaders::intValue(QAnyStringView name) const

返回第一个有效标头name 的值,该值被解释为一个64位整数。如果该标头不存在或无法解析为整数,则返回std::nullopt 。

该函数在 Qt 6.10 中引入。

另请参阅 intValues(QAnyStringView name) 和intValueAt(qsizetype i)。

[noexcept, since 6.10] std::optional<qint64> QHttpHeaders::intValue(QHttpHeaders::WellKnownHeader name) const

该函数重载了QHttpHeaders::intValue (QAnyStringView)。

该函数于 Qt 6.10 版本中引入。

[noexcept, since 6.10] std::optional<qint64> QHttpHeaders::intValueAt(qsizetype i) const

返回索引i 处被解释为64位整数的头值。索引i 必须有效。

该函数在 Qt 6.10 中引入。

另请参阅 intValues(QAnyStringView name) 和intValue(QAnyStringView name)。

[since 6.10] std::optional<QList<qint64>> QHttpHeaders::intValues(QAnyStringView name) const

返回将标头name 解析为64位整数后的值,并以列表形式返回。如果该标头不存在或无法解析为整数,则返回std::nullopt 。

该函数在 Qt 6.10 中引入。

另请参阅 intValue(QAnyStringView name) 和intValueAt(qsizetype i)。

[since 6.10] std::optional<QList<qint64>> QHttpHeaders::intValues(QHttpHeaders::WellKnownHeader name) const

该函数重载了QHttpHeaders::intValues (QAnyStringView)。

该函数于 Qt 6.10 版本中引入。

[noexcept] bool QHttpHeaders::isEmpty() const

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

另请参阅 size()。

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

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

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

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

[since 6.12] std::optional<QHttpHeaderRangeSet> QHttpHeaders::rangeValues() const

返回Range HTTP 头字段的范围,如果解析失败,则返回std::nullopt 。

每个范围代表一个字节范围。根据 RFC 9110:

  • 如果指定了起始位置但未指定结束位置(例如,“bytes=500-”),则QHttpHeaderRangeSpec 将包含start=500 和end=std::nullopt 。
  • 如果指定了结束点但未指定起始点(例如,“bytes=-500”),则QHttpHeaderRangeSpec 将包含start=std::nullopt 和end=500 ,代表最后 500 个字节。
  • 如果两者都指定了(例如,“bytes=0-499”),则QHttpHeaderRangeSpec 将包含start=0 和end=499 。

这些范围将按其在标头中出现的顺序返回。

解析行为如下:

  • 如果不存在Range 标头,则返回一个空范围集。
  • 根据 RFC 9110 第 14.2 节,任何包含除“bytes”以外单位的Range 标头(例如“seconds=1-2”)都将被忽略。这些被忽略的标头不会导致解析失败。
  • 如果Range 标头使用了“bytes”单位但格式不正确(例如缺少连字符、包含无效字符或无效数字),则该函数返回std::nullopt 。

该函数在 Qt 6.12 中引入。

另请参阅 setRangeValues 和WellKnownHeader::Range 。

void QHttpHeaders::removeAll(QAnyStringView name)

删除name 头部。

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

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

该函数重载了QHttpHeaders::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)

将索引为i 的标头条目替换为name 和newValue 。该索引必须有效(参见size())。返回替换是否成功。

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

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

该函数重载了QHttpHeaders::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)

该函数重载了 QHttpHeaders::replaceOrAppend(WellKnownHeader, QAnyStringView)。

void QHttpHeaders::reserve(qsizetype size)

尝试为至少size 个头条目分配内存。

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

[since 6.10] void QHttpHeaders::setDateTimeValue(QAnyStringView name, const QDateTime &dateTime)

将标头名称name 的值设置为dateTime ,遵循标准的HTTP IMF-fixdate格式。如果该标头不存在,则添加一个新的标头。

该函数在 Qt 6.10 中引入。

另请参阅 dateTimeValue(QAnyStringView name) 和dateTimeValueAt(qsizetype i)。

[since 6.10] void QHttpHeaders::setDateTimeValue(QHttpHeaders::WellKnownHeader name, const QDateTime &dateTime)

该函数重载了 QHttpHeaders::setDateTimeValue(QAnyStringView)。

该函数于 Qt 6.10 中引入。

[since 6.12] void QHttpHeaders::setRangeValues(const QHttpHeaderRangeSet &ranges)

将Range HTTP头字段设置为指定的ranges 列表。

这些范围采用“字节”单位进行格式化,并按ranges 中存储的顺序排列。对于每个QHttpHeaderRangeSpec :

  • 仅包含起始点的范围(例如,QHttpHeaderRangeSpec(500, std::nullopt))将格式化为"500-" 。
  • 仅包含结束点的范围(例如,QHttpHeaderRangeSpec(std::nullopt, 500))将格式化为"-500" ,表示最后 500 字节。
  • 同时包含起始和结束的范围(例如,QHttpHeaderRangeSpec(0, 499))将格式化为"0-499" 。

如果提供了多个范围,它们将用逗号分隔,例如:"bytes=0-499, 1000-”。

如果 `ranges ` 为空,则会移除 `Range ` 头部。

该函数自 Qt 6.12 起引入。

另请参阅 rangeValues() 和WellKnownHeader::Range 。

[noexcept] qsizetype QHttpHeaders::size() const

返回标头条目的数量。

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

将此链接QHttpHeaders 替换为other 。此操作非常快,且从未失败。

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

该函数重载了QHttpHeaders::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

该函数重载了QHttpHeaders::values (QAnyStringView)。

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

返回一个与给定的name 对应的视图形式的标头名称。

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

该操作将other 进行移入赋值,并返回对此对象的引用。

other empty 将被保留。

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

将other 的内容赋值给该对象,并返回对此对象的引用。

相关的非成员

[since 6.12] QDataStream &operator<<(QDataStream &out, const QHttpHeaders &headers)

将headers 写入out 流。

该函数在 Qt 6.12 中引入。

另请参阅 《Qt 数据类型的序列化》。

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

将headers 写入debug 流中。

[since 6.12] QDataStream &operator>>(QDataStream &in, QHttpHeaders &headers)

从流in 中读取标头,并将其写入headers 。

该函数在 Qt 6.12 中引入。

另请参阅 《Qt 数据类型的序列化》。

© 2026 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.