zoukankan      html  css  js  c++  java
  • 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字母、数字、~!*()'

    参考资料

  • 相关阅读:
    swagger,参数,list,swaggerui测试list<string>类型参数示例
    c# 获取路径的方法详解
    The assembly for System.Buffers could not be loaded;无法加载System.Buffers的程序集
    把.net Core 项目迁移到VS2019 for MAC
    Asp.net Core 3.0 Identity 使用smtp账户确认和密码恢复
    Mui使用jquery并且使用点击跳转新窗口
    运行第一个abp项目VS2015+localDB
    Asp.net MVC+Bootstrap3的悬浮式登录框效果
    MVC配置ckeditor+ckfinder
    EF 数据初始化
  • 原文地址:https://www.cnblogs.com/fuhongwei041/p/7606577.html
Copyright © 2011-2022 走看看