在 URL 中,空格应该使用 %20 还是 + 进行编码?
在 URL 中,我应该使用 %20
还是 +
对空格进行编码? 例如,在下面的例子中,哪一个是正确的?
www.mydomain.com?type=xbox%20360
www.mydomain.com?type=xbox+360
我们公司倾向于前者,但是使用Java方法URLEncoder.encode(String, String)
与 "xbox 360"
(和 "UTF-8"
) 返回后者。
那么,有什么区别呢?
In a URL, should I encode the spaces using %20
or +
? For example, in the following example, which one is correct?
www.mydomain.com?type=xbox%20360
www.mydomain.com?type=xbox+360
Our company is leaning to the former, but using the Java method URLEncoder.encode(String, String)
with "xbox 360"
(and "UTF-8"
) returns the latter.
So, what's the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用其中任何一个 - 这意味着大多数人选择“+”,因为它更易于人类阅读。
You can use either - which means most people opt for "+" as it's more human readable.
当对查询值进行编码时,无论是形式 plus 还是%-20 都是有效的; 但是,由于互联网的带宽不是无限的,因此您应该使用 plus,因为它少了两个字节。
When encoding query values, either form, plus or percent-20, is valid; however, since the bandwidth of the internet isn't infinite, you should use plus, since it's two fewer bytes.
这应该并不重要,就像您将字母 A 编码为 %41 一样。
然而,如果您正在处理一个无法识别某种形式的系统,那么无论“规范”怎么说,您似乎都必须满足它的期望。
It shouldn't matter, any more than if you encoded the letter A as %41.
However, if you're dealing with a system that doesn't recognize one form, it seems like you're just going to have to give it what it expects regardless of what the "spec" says.
根据 W3C (它们是这些事情的官方来源) ,查询字符串中的空格字符(并且仅在查询字符串中)可以编码为“
%20
”或“+
”。 从“建议”下的“查询字符串”部分:根据一般 URI 的官方规范 RFC2396 的第 3.4 节,“查询" 组件依赖于 URL:
因此,如果其他软件不接受查询字符串中带有编码为“
+
”字符的空格的 URL,则它是一个错误。至于问题的第三部分,修复
URLEncoder.encode()
输出的一种方法(虽然有点难看)是 调用replaceAll( "\\+","%20")
返回值。According to the W3C (and they are the official source on these things), a space character in the query string (and in the query string only) may be encoded as either "
%20
" or "+
". From the section "Query strings" under "Recommendations":According to section 3.4 of RFC2396 which is the official specification on URIs in general, the "query" component is URL-dependent:
It is therefore a bug in the other software if it does not accept URLs with spaces in the query string encoded as "
+
" characters.As for the third part of your question, one way (though slightly ugly) to fix the output from
URLEncoder.encode()
is to then callreplaceAll("\\+","%20")
on the return value.表单数据(对于 GET 或 POST)通常编码为
application/x-www-form-urlencoded
:这指定+
表示空格。URL 编码为 RFC 1738,其中指定
%20
。理论上我认为你应该在
?
之前添加 %20 ,在之后添加 + :Form data (for GET or POST) is usually encoded as
application/x-www-form-urlencoded
: this specifies+
for spaces.URLs are encoded as RFC 1738 which specifies
%20
.In theory I think you should have %20 before the
?
and + after: