java,javascript中的url编码

真实场景

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字母、数字、~!*()'

参考资料

posted on 2017-09-28 14:36  James.H.Fu  阅读(465)  评论(0编辑  收藏  举报

导航