Java HTTP 标头中的非法字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19028068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal characters in HTTP headers
提问by Svish
I'm creating an HttpUrlConnection
and need to set multiple custom headers.
我正在创建一个HttpUrlConnection
并且需要设置多个自定义标题。
I'd like to do something along the lines of the following, but the contents of the header map needs to come from a single string. Are there any characters that are illegal or extremely rarely used in both HTTP header names and HTTP header values?
我想按照以下方式做一些事情,但标题映射的内容需要来自单个字符串。在 HTTP 标头名称和 HTTP 标头值中是否有任何非法或极少使用的字符?
HashMap<String, String> headers = new HashMap<String, String>();
// TODO: How can I fill the headers map reliably from a single string?
HttpURLConnection c = (HttpURLConnection) url.openConnection();
for(Map.Entry<String, String> e : headers.entrySet())
c.setRequestProperty(e.getKey(), e.getValue());
Solution for now
暂时解决方案
Doesn't seem like any HTTP header names contain any spaces (usually use dash instead?), so I can separate the name with the value using a single space. As for the name-value sets, it seems I'm screwed since the value can contain pretty much anything according to the given answer. So I've just picked a character I'm pretty sure will most likely never be used: §
. If it turns out it is actually needed, I'll just have to adjust my code :p
似乎任何 HTTP 标头名称都不包含任何空格(通常使用破折号代替?),因此我可以使用单个空格将名称与值分开。至于名称-值集,似乎我被搞砸了,因为根据给定的答案,该值几乎可以包含任何内容。所以我刚刚选择了一个我很确定很可能永远不会使用的字符:§
. 如果事实证明确实需要它,我只需要调整我的代码:p
Header1 Value1§Header2 Value2§Header3 Header3
回答by mc0e
The relevant BNF from RFC7230 is:
来自 RFC7230 的相关 BNF 是:
field-name = token
token = 1*tchar
tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" /
"." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
The character set is visible USASCII.
字符集是可见的 USASCII。
RFC 7230 is more recent than your question, but in the relevant particulars, it does not change what was formerly said by RFC 2616.
RFC 7230 比您的问题更新,但在相关细节中,它不会改变 RFC 2616 之前所说的内容。
There's a very strong convention for field names which is much more restrictive than what the RFC allows, and this is enforced to various degrees in various implementations. Field Names usually follow a pattern of a sequence of [ASCII / NUMERAL] words with the first letter (only) of each word being capitalised. The words are separated with a single hyphen.
字段名称有一个非常强大的约定,它比 RFC 所允许的要严格得多,并且在各种实现中都不同程度地强制执行。字段名称通常遵循 [ASCII / NUMERAL] 单词序列的模式,每个单词的第一个字母(仅)大写。这些单词用一个连字符分隔。
So, for example "HttpUrlConnection" was supposed to be an HTTP Header name (rather than a java token), you'd call it 'Http-Url-Connection'.
因此,例如“HttpUrlConnection”应该是 HTTP 标头名称(而不是 Java 令牌),您可以将其称为“Http-Url-Connection”。
I dimly remember once tracking a bug down to some implementation being strict enough not to admit multiple capitals in one word (which happened to be an acronym). I.e. it pays to follow this more restricted format very strictly.
我依稀记得曾经将一个错误追踪到某个实现足够严格以至于不能在一个单词中承认多个大写字母(这恰好是一个首字母缩写词)。即非常严格地遵循这种更受限制的格式是值得的。
Non ASCII character sets play no part in field-names, though they may be used in field values.
Escaping in field names is not supported by the standard. Escaping of values is not hte concern of the HTTP or MIME standards, but you could choose to reuse the standard URL encoding method for encoding a set of name value pairs.
非 ASCII 字符集在字段名中没有作用,尽管它们可以在字段值中使用。
标准不支持在字段名称中转义。值的转义不是 HTTP 或 MIME 标准的问题,但您可以选择重用标准 URL 编码方法来对一组名称值对进行编码。