HTTP标头密钥可以重复吗? [英] HTTP Header Key can be repeated?
问题描述
在JAVA HttpUrlConnection中,请求标头设置的主要逻辑代码如下:
In JAVA HttpUrlConnection , the main logic code of request Header settings as following:
public synchronized void set(String k, String v) {
for (int i = nkeys; --i >= 0;)
if (k.equalsIgnoreCase(keys[i])) {
values[i] = v;
return;
}
add(k, v);
}
已验证密钥应该是唯一的,密钥必须与值保持一对一的映射关系.
It is verified that the key should be unique, the key has to keep one to one mapping relationship with the value.
相反,在Response模块的HeaderFields中,结构定义为Entry>.也就是说,键不与值保持一对一的映射关系.
On the contrary, in HeaderFields of Response module, structure is defined as Entry >. That is, the key does not keep one to one mapping relationship with the value.
这是为什么? HTTP协议是否有相关协议?
Why is this? Does the HTTP protocol has relevant agreement?
添加: 在HttpClient4中,请求标头设置的主要逻辑代码如下:
Add: In HttpClient4 ,the main logic code of request Header settings as following:
/**
* Replaces the first occurence of the header with the same name. If no header with
* the same name is found the given header is added to the end of the list.
*
* @param header the new header that should replace the first header with the same
* name if present in the list.
*/
public void updateHeader(final Header header) {
if (header == null) {
return;
}
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header current = this.headers.get(i);
if (current.getName().equalsIgnoreCase(header.getName())) {
this.headers.set(i, header);
return;
}
}
this.headers.add(header);
}
响应标题
/**
* Gets all of the headers with the given name. The returned array
* maintains the relative order in which the headers were added.
*
* <p>Header name comparison is case insensitive.
*
* @param name the name of the header(s) to get
*
* @return an array of length >= 0
*/
public Header[] getHeaders(final String name) {
final List<Header> headersFound = new ArrayList<Header>();
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header header = this.headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
headersFound.add(header);
}
}
return headersFound.toArray(new Header[headersFound.size()]);
}
它们与HttpUrlConnection相同
They are the same of HttpUrlConnection
推荐答案
HTTP协议是否有相关协议?
Does the HTTP protocol has relevant agreement?
是的. RFC 2616第4.2节消息标题" 说:
多个具有相同字段名称的消息标题字段可能是 在消息中出现,当且仅当该消息的整个字段值 标头字段定义为以逗号分隔的列表[即#(values)]. 必须可以将多个标头字段组合为一个 "field-name:field-value"对,而无需更改 消息,方法是将每个后续字段值附加到第一个 以逗号分隔.标头字段具有相同顺序 因此,接收到的字段名称对 组合字段值的解释,因此不能使用代理 转发消息时,更改这些字段值的顺序.
Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.
发件人不得生成具有相同字段的多个标头字段 消息中的名称,除非该消息的整个字段值 标头字段定义为以逗号分隔的列表[即#(values)] 或标头字段是众所周知的例外情况(如下所述).
A sender MUST NOT generate multiple header fields with the same field name in a message unless either the entire field value for that header field is defined as a comma-separated list [i.e., #(values)] or the header field is a well-known exception (as noted below).
收件人可以将多个标头字段与同一字段组合 名称分成一对字段名称:field-value",而无需更改 消息的语义,方法是将每个后续字段值附加到 按顺序组合的字段值,以逗号分隔. 在其中接收具有相同字段名称的标头字段是 因此对于组合场的解释很重要 价值;代理不得在以下情况下更改这些字段值的顺序 转发消息.
A recipient MAY combine multiple header fields with the same field name into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma. The order in which header fields with the same field name are received is therefore significant to the interpretation of the combined field value; a proxy MUST NOT change the order of these field values when forwarding a message.
这篇关于HTTP标头密钥可以重复吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!