URLEncode and URLDecode

 

 

When submitting a form, the browser will generate a corresponding request message to the server according to the request URL. In this process, the browser will convert the request address to application/x-www-form-urlencoded MIME encoded string first, as shown in the following figure, the Chinese character "Chen" in utf-8 is converted into "Chen" in this encoding specification. %E9%99%88”

 

The application/x-www-form-urlencoded encoding specification is:

1. The characters 'a'-'z', 'A'-'Z', '0'-'9' remain unchanged

2. Special characters '.', '-', '*', '_' remain unchanged

3. Convert the empty character " " to "+"

4. Other characters are regarded as unsafe and are converted. Each byte is converted into the form of %xy according to some rules, where 'xy' is two hexadecimal numbers representing a byte.

 

Explain with the above example. The unicode code of "Chen" is 0x9648, and the binary representation is 1001 0110 0100 1000. In utf-8 encoding, 3 bytes are needed to represent: 11101001 10011001 10001000. In hexadecimal, these three bytes are E9, 99, and 88.

 

URLEncode and URLDecode are the encoding and decoding classes that implement this specification. When looking at the source code of URLEncode, I got stuck twice. One is the unicode code of "Chen" returned by String.charAt(i), but think about it, char is two bytes, of course. It is impossible to return the binary of three bytes (String internally maintains a char array). The other is the char Character.forDigit(int digit, int radix) method. The parameter radix of this method is a value >=2 && <=36. The radix represents the base, and 10 is the decimal. digit is a digit >0 && <radix. The function of this method is to specify the expression form of the next number in the base system, and connect it with lowercase letters if it exceeds 9. For example, Character.forDigit(11,16) returns the character 'b'.

There is also the development of URLEncode. The process of writing code by Herb Jellinek can be understood by using System.out.println() test, but it is a bit ugly to directly comment the print code and submit it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326290812&siteId=291194637