urlEncode urlDecode源码解析

1. 背景

url中可能包含一些字符如中文呀,&、空格、?等,是不符合浏览器规范的,需要使用urlencode转换成可以传输的字符

在源码里可以看到A-Za-z0-9 - _ . * 是不需要转换的,特殊的空格会变成+号

BItset dontNeedEncoding =new BitSet(256);

int i;

for (i ='a'; i <='z'; i++) {

dontNeedEncoding.set(i);

}

for (i ='A'; i <='Z'; i++) {

dontNeedEncoding.set(i);

}

for (i ='0'; i <='9'; i++) {

dontNeedEncoding.set(i);

}

dontNeedEncoding.set(' ');/* encoding a space to a + is done 

* in the encode() method */

dontNeedEncoding.set('-');

dontNeedEncoding.set('_');

dontNeedEncoding.set('.');

dontNeedEncoding.set('*');

2 encode原理

将不符合规范的字符转成16进制并在前面加上%,如) 转成了%29

特殊的空格会变成+

3.decode原理

遇到%xy的,一定是需要反编译的。将%后面的两位从16进制转换出来即可。

public static String decode(String s, String enc) throws UnsupportedEncodingException{ boolean needToChange = false; int numChars = s.length(); StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars); int i = 0; if (enc.length() == 0) { throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter"); } char c; byte[] bytes = null; while (i < numChars) { c = s.charAt(i); switch (c) { case '+': sb.append(' '); i++; needToChange = true; break; case '%': /* * Starting with this instance of %, process all * consecutive substrings of the form %xy. Each * substring %xy will yield a byte. Convert all * consecutive bytes obtained this way to whatever * character(s) they represent in the provided * encoding. */ try { // (numChars-i)/3 is an upper bound for the number // of remaining bytes if (bytes == null) bytes = new byte[(numChars-i)/3]; int pos = 0; while ( ((i+2) < numChars) && (c=='%')) { int v = Integer.parseInt(s.substring(i+1,i+3),16); if (v < 0) throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value"); bytes[pos++] = (byte) v; i+= 3; if (i < numChars) c = s.charAt(i); } // A trailing, incomplete byte encoding such as // "%x" will cause an exception to be thrown if ((i < numChars) && (c=='%')) throw new IllegalArgumentException( "URLDecoder: Incomplete trailing escape (%) pattern"); sb.append(new String(bytes, 0, pos, enc)); } catch (NumberFormatException e) { throw new IllegalArgumentException( "URLDecoder: Illegal hex characters in escape (%) pattern - " + e.getMessage()); } needToChange = true; break; default: sb.append(c); i++; break; } } return (needToChange? sb.toString() : s);}

你可能感兴趣的:(urlEncode urlDecode源码解析)