zoukankan      html  css  js  c++  java
  • url中特殊字符被转义成编码后处理

    开发时有时服务端返回的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);
    
        }
    
    }

  • 相关阅读:
    <转载> diff 和 patch 命令
    JZ2440开发板之LCD
    发音篇--第一章
    JZ2440开发板之系统始终和串口
    【还是用回csdn了,这个blog就不更新了】
    MyBatis 汉字作为查询条件查询不到 MySQL 中的结果
    LeetCode 617. 合并二叉树 Merge Two Binary Tree
    LeetCode 454. 四数相加 II 4Sum II
    LeetCode 441. 排列硬币 Arranging Coins
    leetcode ——从排序数组中删除重复项 II
  • 原文地址:https://www.cnblogs.com/xiejn/p/14156666.html
Copyright © 2011-2022 走看看