冒号是否需要在URI查询参数中进行编码? [英] Do colons require encoding in URI query parameters?
问题描述
我注意到Java的 UriBuilder
不会对我的查询参数值中包含的:
字符进行编码( ISO 8601格式的字符串)。
I've noticed that Java's UriBuilder
isn't encoding the :
characters included in my query parameter values (ISO 8601-formatted strings).
根据维基百科,似乎冒号应该被编码。
According to Wikipedia, it seems colon should be encoded.
特别是,对查询字符串进行编码时使用以下规则:
In particular, encoding the query string uses the following rules:
- 字母(AZ和az),数字(0- 9)和字符'。',' - ','〜'和'_'保持原样
- SPACE被编码为'+'或%20 [需要引证]
- 所有其他字符编码为%FF十六进制表示,其中任何非ASCII字符首先编码为UTF-8(或其他指定的
编码)
那么,这笔交易是什么?查询参数中的冒号是否应该编码?
So, what's the deal? Should colons in query parameters be encoded or not?
更新:
我查找了URI语法规范( RFC 3986 )看起来像查询参数中的冒号实际上并不是必需的。以下是ABNF对URI的摘录:
I looked up the URI Syntax spec (RFC 3986) and it looks like encoding colons in query params really isn't necessary. Here's an excerpt from the ABNF for URI:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=
推荐答案
是的,它们应编码为查询字符串。正确的编码是%3A
Yes, they should be encoded in a query string. The correct encoding is %3A
但是,我可以理解为什么UriBuilder没有编码:
。您不希望在协议(例如 http:
)之后或用户名和密码之间对冒号进行编码(例如 ftp:// username:password@domain.com
)绝对URI。
However, I can understand why UriBuilder isn't encoding :
. You don't want to encode the colon after the protocol (eg http:
) or between the username and password (eg ftp://username:password@domain.com
) in an absolute URI.
这篇关于冒号是否需要在URI查询参数中进行编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!