在使用 url 的 queryString 传递参数时,因为参数的值,被DES加密了,而加密得到的是 Base64的编码字符串,类似于:

za4T8MHB/6mhmYgXB7IntyyOUL7Cl++0jv5rFxAIFVji8GDrcf+k8g==

显然 这里面含有了 特殊字符: / + = 等等,如果直接通过url 来传递该参数:

url = "xxxxx?param=" + "za4T8MHB/6mhmYgXB7IntyyOUL7Cl++0jv5rFxAIFVji8GDrcf+k8g==";

那么在服务端获得 param 会变成类似于下面的值:

"za4T8MHB/6mhmYgXB7IntyyOUL7Cl  0jv5rFxAIFVji8GDrcf k8g=="

我们看到 三个 + 号消失了。

其原因就是:如果url参数值含有特殊字符时,需要使用 url 编码。

url = "xxxxx?param=" + URLEncoder.encode("xxx", "utf-8");

然后服务端获取时:

String param = URLDecoder.decode(param, "utf-8");

这样才能获得正确的值:"za4T8MHB/6mhmYgXB7IntyyOUL7Cl++0jv5rFxAIFVji8GDrcf+k8g=="

其实 js 中也有类似功能的函数:

参见:js中的三个编码函数:escape,encodeURI,encodeURIComponent

注意事项

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =

String q = "random word 拢500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

URLEncoder 必须 仅仅 编码 参数 或者参数的值,不能编码整个 url,也不能一起对 param=value 进行编码。而是应该: param=URLEncode(value, "utf-8")

或者 URLEncode(param, "utf-8")=URLEncode(value, "utf-8")

因为 url 中的 & 和 = 他们是作为参数之间 以及 参数和值之间的分隔符的。如果一起编码了,就无法区分他们了。

进一步参考文档:

https://www.talisman.org/~erlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/

最新文章

  1. 框架Hibernate笔记系列 基础Session
  2. pecl install imagick
  3. 推荐:一个个人开发者搞app赚钱之后的总结!有图有真相。
  4. 【20】宁以pass-by-reference-to-const替换pass-by-value
  5. Android中写入读取XML
  6. mysql----用户root被删除或忘记root密码的解决方案
  7. 11427 - Expect the Expected(概率期望)
  8. gulp+browser-sync使用方法
  9. java :instanceof用法
  10. JVM命令
  11. JVM学习笔记一:Java运行时数据区域
  12. ios滑动流畅(丝般顺滑)滚动
  13. JsonConvert.DeserializeObject反序列化
  14. oracle 11.2.0.4 rac 修改 ip vip scan ip
  15. BaseDao.util(虎大将军)
  16. 理解Java注解类型
  17. 使用Junit进行单元测试
  18. VS Code Html Zen coding
  19. gaea-editor 知识点
  20. linux ssh 连接设置

热门文章

  1. WebGIS中GeoHash编码的研究和扩展
  2. (十八)WebGIS中清空功能和地图定位功能的设计以及实现
  3. Android Studio获取SHA1和MD5方法
  4. 认识Java Core和Heap Dump
  5. null和undefined的一些区别
  6. 尝试加载 Oracle 客户端库时引发 BadImageFormatException
  7. Spring注入中byType和byName的总结
  8. POJ-3061
  9. bzoj 1179[Apio2009]Atm (tarjan+spfa)
  10. Linux中shell脚本自动输入密码