url中特殊字符被转义成编码后处理

2020-12-23 19:26

阅读:441

标签:alt   http   signed   包含   ring   图片   decode   urlencode   rtt   

开发时有时服务端返回的json中包含url,url中可能含有一些特殊字符,这些特殊字符在传输的过程中可能会被转义成编码。这时候我们拿到手里要如何转换回去呢,先看下那些字符可能会被编码

例:

String url = "http://www.baidu.com/signedUrl%3Fsignature=eyJhbGciOiJSUzI1NiJ9.ey";
String url2 = "http://192.168.73.88:9099/hefei/decision/road/cartrack?type=bus%26carId=1123%26startTime=2020-12-18%2012:10:01%26endTime=2020-12-18%2014:20:16";

这里面的 %3F 应该是 ? ,%26 是 & ,那么如何转回去

package com.x;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "http://127.0.0.1:9099/hefei/decision/road/cartrack?type=bus%26carId=1123%26startTime=2020-12-18%26endTime=2020-12-18";
        String url2 = "http://192.168.73.88:9099/hefei/decision/road/cartrack?type=bus%26carId=1123%26startTime=2020-12-18%2012:10:01%26endTime=2020-12-18%2014:20:16";
        String deurl = URLDecoder.decode(url, "UTF-8");
        String deurl2 = URLDecoder.decode(url2, "UTF-8");
        System.out.println("deurl:" + deurl);
        System.out.println("deurl2:" + deurl2);

        System.out.println();

        // 再次decode不会影响结果
        String decode = URLDecoder.decode(deurl, "UTF-8");
        String decode2 = URLDecoder.decode(deurl2, "UTF-8");
        System.out.println("decode:" + decode);
        System.out.println("decode2:" + decode2);

        System.out.println();

        String encodeUrl = URLEncoder.encode(deurl, "UTF-8");
        System.out.println("encodeUrl:" + encodeUrl);
        String decodeUrl = URLDecoder.decode(encodeUrl, "UTF-8");
        System.out.println("decodeUrl:" + decodeUrl);

    }

}

技术图片

 

url中特殊字符被转义成编码后处理

标签:alt   http   signed   包含   ring   图片   decode   urlencode   rtt   

原文地址:https://www.cnblogs.com/xiejn/p/14156666.html


评论


亲,登录后才可以留言!