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

e52ac04f9c6ddb864156ba90af6343a0.png

慕田峪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的区别”,搜出来的博文基本只提到了对"+"处理的区别,并未提到“~”

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值