url编码问题

url编码规则

1.字符"a"-“z”,“A”-“Z”,“0”-“9”,".","-","*",和"_" 都不会被编码;
2.将空格转换为加号 (+) ;
3.将非文本内容转换成"%xy"的形式,xy是两位16进制的数值;

可以直接使用URLEncoder.encode()和URLDecoder.decode()进行编码和解码

String s = ".a.b&c-d+e*f/g@h#i$j%k^l(m)n o,p\\s[t]u`";
try {
	String urls = URLEncoder.encode(s, "UTF-8");
	System.out.println(urls);
} catch (UnsupportedEncodingException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

对特殊字符进行编码

public String getUrlString(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
        	if(bs[i]>=97 && bs[i]<=122) {
        		sb.append(c[i]);
        		continue;
        	}else if(bs[i]>=65 && bs[i]<=90) {
        		sb.append(c[i]);
        		continue;
        	}else if(bs[i]>=48 && bs[i]<=57) {
        		sb.append(c[i]);
        		continue;
        	}else {
	            bit = (bs[i] & 0x0f0) >> 4;
	            sb.append("%"+chars[bit]);
	            bit = bs[i] & 0x0f;
	            sb.append(chars[bit]);
        	}
        }
        return sb.toString().trim();
    }

参考文章: https://blog.csdn.net/qq_36750461/article/details/84846737.

你可能感兴趣的:(gitlab,api,java,url,gitlab,api)