浏览器 url 编码与 java 的 URLEncoder.encode 不同

发布于 2024-08-15 16:02:43 字数 537 浏览 4 评论 0原文

我有一个像这样的网址: Product//excerpts?feature=picture+modes

当我从浏览器访问此网址时,后端收到的请求为: “product//excerpts?feature=picture+modes”

在网络服务器端,我使用 + 作为分隔符。所以 feature=picture+modes 意味着有 2 个功能:

我创建的图片和模式以及自动脚本(Java),它会转到 url 并检索其内容。 当我运行此脚本时,后端收到的请求为: “product/B000NK6J6Q/excerpts/feature=picture%2Bmodes”

这是因为在我的脚本(Java)中我使用 URLEncoder.encode 将 + 转换为 %2B 并将此编码的 url 发送到服务器。

为什么Java提供的urlEncoder和浏览器(FF/IE)提供的urlEncoder不同? 我如何使它们相同?我如何解码它们? (URLDecoder.decode 上的“+”给出了空格) 另外,使用“+”作为分隔符是否符合约定(和规范?)?

普拉克

I have a url like this:
product//excerpts?feature=picture+modes

when i access this url from browser, the backend receives the request as:
"product//excerpts?feature=picture+modes"

At the web server end, i am using + as a separator. so feature=picture+modes means there are 2 features: picture and modes

I have created and automated script(Java) which goes to the url and retrieves its content.
When i run this script, the backend receives the request as:
"product/B000NK6J6Q/excerpts/feature=picture%2Bmodes"

This is because inside my script(Java) i use URLEncoder.encode which converts + to %2B and send this encoded url to the server.

Why are the urlEncoders provided by Java and those present with browsers(FF/IE) different.
how do i make them same? How do i decode them? ('+' on URLDecoder.decode gives space)
Also, is using '+' as a separator according to conventions (and specifications?) ?

Prac

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱的故事 2024-08-22 16:02:43

你所看到的实际上是正确的。请参阅 Wikipedia 上的百分比编码+ 字符是保留字符,因此需要编码为 %2B。此外,历史上浏览器对使用 MIME 类型 application/x-www-form-urlencoded 提交的表单使用不同形式的百分比编码,其中空格变为 + 而不是 <代码>%20。

如果您想在 URL 中使用 +,那么您的分隔符应该是后端的空格。如果您想在后端使用 +,则 URL 中将包含 %2B

What you are seeing is actually correct. See Percent encoding on Wikipedia. The + character is a reserved character and therefore needs to be encoded as %2B. Furthermore, historically browsers use a different form of percent encoding for forms that are submitted with the MIME type application/x-www-form-urlencoded where spaces become + instead of %20.

If you want to use a + in the URL then your separator should be a space in the backend. If you want to use + in the backend, then you will have %2B in the URL.

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