浏览器 url 编码与 java 的 URLEncoder.encode 不同
我有一个像这样的网址: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所看到的实际上是正确的。请参阅 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 typeapplication/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.