冒号是否需要在 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:
- 字母(A-Z 和 a-z)、数字 (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屋!