码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-12-23 12:24:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!