慕田峪4524236
其实是有区别的,php的urlencode会把星号*转为%2A, java的则保留。以前对接接口的时候踩过这个坑,因为urlencode结果不一致导致验签不通过
以下是php和java对RFC2396规定的9个非保留符号的处理结果,可以看到对*和~的处理存在区别
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的执行结果
可见urlencode与URLEncoder在对非保留字符处理的区别仅体现在符号"*"上。值得注意的是百度搜索搜“urlencode与rawurlencode的区别”,搜出来的博文基本只提到了对"+"处理的区别,并未提到“~”
以上代码运行结果基于某在线工具,未排除运行在不同环境(语言版本)下结果不一致的可能性