在 URL 中,空格应该使用 %20 还是 + 进行编码?

发布于 2024-07-29 23:33:22 字数 527 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡紫姑娘! 2024-08-05 23:33:22

您可以使用其中任何一个 - 这意味着大多数人选择“+”,因为它更易于人类阅读。

You can use either - which means most people opt for "+" as it's more human readable.

转身泪倾城 2024-08-05 23:33:22

当对查询值进行编码时,无论是形式 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.

骄傲 2024-08-05 23:33:22

应该并不重要,就像您将字母 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.

多情癖 2024-08-05 23:33:22

根据 W3C (它们是这些事情的官方来源) ,查询字符串中的空格字符(并且仅在查询字符串中)可以编码为“%20”或“+”。 从“建议”下的“查询字符串”部分:

在查询字符串中,加号被保留作为空格的速记符号。 因此,必须对实数加号进行编码。 此方法用于使查询 URI 更容易在不允许空格的系统中传递。

根据一般 URI 的官方规范 RFC2396 的第 3.4 节,“查询" 组件依赖于 URL:

3.4。 查询组件
查询组件是要解释的一串信息
资源。

 查询 = *uric 
  

在查询组件中,字符“;”、“/”、“?”、“:”、“@”、
“&”、“=”、“+”、“,”和“$”被保留。

因此,如果其他软件不接受查询字符串中带有编码为“+”字符的空格的 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":

Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.

According to section 3.4 of RFC2396 which is the official specification on URIs in general, the "query" component is URL-dependent:

3.4. Query Component
The query component is a string of information to be interpreted by
the resource.

   query         = *uric

Within a query component, the characters ";", "/", "?", ":", "@",
"&", "=", "+", ",", and "$" are reserved.

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 call replaceAll("\\+","%20") on the return value.

如此安好 2024-08-05 23:33:22

表单数据(对于 GET 或 POST)通常编码为 application/x-www-form-urlencoded:这指定 + 表示空格。

URL 编码为 RFC 1738,其中指定 %20

理论上我认为你应该在 ? 之前添加 %20 ,在之后添加 + :

example.com/foo%20bar?foo+bar

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:

example.com/foo%20bar?foo+bar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文