java URLEncoder.encode 和 PHP urlencode 是一样的吗?

java URLEncoder.encode 和 PHP urlencode 是一样的吗?

如何不是 那么他和那个PHP函数有同样的效果呢 谢谢

阅读 6.3k
2 个回答

其实是有区别的,php的urlencode会把星号*转为%2A, java的则保留。以前对接接口的时候踩过这个坑,因为urlencode结果不一致导致验签不通过

以下是php和java对RFC2396规定的9个非保留符号的处理结果,可以看到对*和~的处理存在区别
php
java

RFC2396对非保留字符的说明

Data characters that are allowed in a URI but do not have a reserved
purpose are called unreserved. These include upper and lower case
letters, decimal digits, and a limited set of punctuation marks and
symbols.

 unreserved  = alphanum | mark

 mark        = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
 

Unreserved characters can be escaped without changing the semantics
of the URI, but this should not be done unless the URI is being used
in a context that does not allow the unescaped character to appear.


补充

上面PHP截图里不小心写成rawurlencode了,既然问的是urlencode, 有必要补充一下urlencode的执行结果
php urlencode

可见urlencode与URLEncoder在对非保留字符处理的区别仅体现在符号"*"上。
值得注意的是百度搜索搜“urlencode与rawurlencode的区别”,搜出来的博文基本只提到了对"+"处理的区别,并未提到“~

以上代码运行结果基于某在线工具,未排除运行在不同环境(语言版本)下结果不一致的可能性

一样的,都是用于url的编码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题