真实场景

url示例如下

http://localhost:31956/Login/Auto?Token=e8a67a9f-c062-4964-b703-d79f29c8b64e&ReturnUrl=/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com

/Login/Auto接收两个查询参数(query string)Token和ReturnUrl, 其中ReturnUrl 的值比较特殊 /mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com ,它内部还有查询参数,其中含有两个特殊字符(?和&)。如果不进行编码处理/Login/Auto会认为自己有三个查询参数,分别是Token,ReturnUrl和UrlReferer。这显然不是我们想要的结果。我们应该对ReturnUrl进行编码处理。

java中使用java.net.URLEncoder.encode进行编码

 @Test
public void testEncode() throws EncoderException, UnsupportedEncodingException {
String rawUrl = "http://www.baidu.com?param=~!@#$&*()=:/,;?+'";
String encodeByURLEncoder = URLEncoder.encode(rawUrl, "utf-8"); System.out.println("java.net.URLEncoder encode(UTF-8):");
System.out.println(encodeByURLEncoder);
}

java中使用commons-codec进行编码

  1. 在pom.xml中增加commons-codec依赖
<!--apache commons-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
  1. 实例化URLCodec进行编码
 @Test
public void testEncodeReturnUrl() throws EncoderException {
String loginUrl = "http://localhost:31956/Login/Auto?Token=%s&ReturnUrl=%s";
String token = "e8a67a9f-c062-4964-b703-d79f29c8b64e";
String returnUrl = "/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com"; URLCodec codec = new URLCodec();
String tokenEncode = codec.encode(token);
String returnUrlEncode = codec.encode(returnUrl); String loginUrlEncode = String.format(loginUrl, tokenEncode, returnUrlEncode);
System.out.println("编码结果:");
System.out.println(loginUrlEncode);
}

编码结果http://localhost:31956/Login/Auto?Token=e8a67a9f-c062-4964-b703-d79f29c8b64e&ReturnUrl=%2Fmobilesite%2FGoodsReceipt%2FJumpSourceIBuild%3FprojectSysNo%3D19%26urlReferer%3Dhttp%3A%2F%2Fwww.baidu.com

javascript中使用encodeURIComponent进行编码

var loginUrl = "http://localhost:31956/Login/Auto?Token=#{1}#&ReturnUrl=#{2}#";
var token = "e8a67a9f-c062-4964-b703-d79f29c8b64e";
var returnUrl = "/mobilesite/GoodsReceipt/JumpSourceIBuild?projectSysNo=19&urlReferer=http://www.baidu.com"; var tokenEncode = encodeURIComponent(token);
var returnUrlEncode = encodeURIComponent(returnUrl); var loginUrlEncode = loginUrl.replace("#{1}#", tokenEncode).replace("#{2}#", returnUrlEncode);
console.log("编码结果:", loginUrlEncode);

javascript中escape, encodeURI, encodeURIComponent的区别

  1. 对字符串(string)进行编码,其中 ASCII字母、数字、@*/+ ,这几个字符不会被编码,其余的都会。
  2. encodeURI方法不会对下列字符编码 ASCII字母、数字、~!@#$&*()=:/,;?+'
  3. encodeURIComponent方法不会对下列字符编码 ASCII字母、数字、~!*()'

参考资料

java,javascript中的url编码的更多相关文章

  1. 详解JavaScript中的Url编码/解码,表单提交中网址编码

    本文主要针对URI编解码的相关问题做了介绍,对Url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript 中和 编解码相关的几对函数escape / unescape ...

  2. Javascript中的url编码与解码(详解)

    摘要 本文主要针对URI编解码的相关问题做了介绍,对url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript中和编解码相关的几对函数escape / unescap ...

  3. 001. Java内存中的字符编码

    Java内存中的字符编码 Unicode字符集及utf-8 .utf-16.utf-32 等字符编码方式 字符集:字符表示的数字集合,元素称为码点或码位: 字符编码:字符实际的储存表示: 码点:一个码 ...

  4. python中的URL编码和解码

    python中的URL编码和解码:test.py # 引入urllib的request模块 import urllib.request url = 'https://www.douban.com/j/ ...

  5. Javascript中对文字编码的三个函数

    JavaScript中对文字编码主要有3个函数 escape,encodeURI, encodeURIComponent 相应3个解码函数 unescape, decodeURI, decodeURI ...

  6. Delphi中处理URL编码解码

    Delphi中处理URL编码解码 一.URL简单介绍     URL是网页的地址,比方 http://www.shanhaiMy.com. Web 浏览器通过 URL 从 web server请求页面 ...

  7. 解决java中对URL编码的问题

    首先查看javascript中的encodeURI和encodeURLComponent方法的区别. encodeURI:不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行 ...

  8. java中的url 编码与解码

    什么是application/x-www-form-urlencoded字符串? 答:它是一种编码类型.当URL地址里包含非西欧字符的字符串时,系统会将这些字符转换成application/x-www ...

  9. Java 8中的Base64编码和解码

    转自:https://juejin.im/post/5c99b2976fb9a070e76376cc Java 8会因为将lambdas,流,新的日期/时间模型和Nashorn JavaScript引 ...

随机推荐

  1. TUM数据集rgbd_benchmark工具的使用方法

    # 在学习视觉slam过程中,需要对数据集合进行预处理和对slam或者跟踪结果进行评价,TUM提供一组这样的工具,为了自己以后方便查找,于是把它记录下来 一.RGBD_Benchmark工具下载链接: ...

  2. CloudSim——云计算仿真软件概述

    CloudSim是由澳大利亚墨尔本大学的网格实验室和Gridbus项目宣布推出的云计算仿真软件. CloudSim是做什么的呢?可以简单理解为一个帮助研究.开发.测试的工具,如虚拟机资源分配算法.节能 ...

  3. [LTR] RankLib.jar 包介绍

    一.介绍 RankLib.jar 是一个学习排名(Learning to rank)算法的库,目前已经实现了如下几种算法: MART RankNet RankBoost AdaRank Coordin ...

  4. LeetCode题解Maximum Binary Tree

    1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...

  5. oracle中给某个用户某张表的权限设置

    今天碰到需要给数据库上某一个用户,开通其中2张表的查询权限,方法如下: grant select on bas_checkcycle to jdc;这个是整个语句. 语句分析: grant selec ...

  6. VS 2015连接SQL server数据库方法

    vs新建一个Windows窗口应用程序,界面布局如下: Form1.cs中代码如下: using System; using System.Collections.Generic; using Sys ...

  7. .NetCore Build Terminology

       .NETCore Command:    1.dotnet build 2.dotnet run 3.dotnet new classlib 4.dotnet new xunit 5.dotne ...

  8. ABAP设计模式——适配器

    计算机科学中的大多数问题都可以通过增加一层间接性来解决.  ——David Wheeler 适配器模式(Adapter Design Pattern),是一个广泛应用于真实世界和面向对象编程语言的设计 ...

  9. Linux 下安装 Tomcat 出现拒绝访问的情况

    此外也无法调用 java -version 查看版本号 ./shutdown 时:提示找不到 JDK 的某个文件夹 ./startup 时:却启动正常 访问 8080 端口时,显示拒绝访问 解决方法: ...

  10. Spring集成MyBatis持久层框架

    一.MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,可以使用简单的XML ...