First, why use Urlencoder
When a client makes a request for a Web page, the URL may contain non-ASCII content, such as Chinese.
It is not allowed to put the Chinese in the URL directly, so we need to use Urlencoder to encode the address.
Converts non-ASCII content in URLs to characters that can be transferred
Content that will not be encoded
1. Capital Letter A-Z
2. Lowercase letter A-Z
3. Number 0-9
4. Punctuation characters-_. ! ~ * ' (and,)
Second, the principle of coding
1, will need to convert the content (outside the ASCII code form of the content), the hexadecimal notation is converted, and preceded by a% of the beginning
eg:0x9c Urlencoder--%9c
2, the contents of the space ", all with + instead of
3, Note: Unlike hex, Hex converts all characters to 16 binary representations, while Urlencoder converts the ASCII code set to% plus the corresponding 16 binary, while the characters in the ASCII set are not processed
Third, the application scenario
1. All GET Requests
2, the website has the Chinese and so on the situation
3, POST request, all key and value before committing to go through Urlencoder
Iv. examples
try {
String a = Urlencoder.encode ("A", "UTF-8");
LOG.I ("Encode", "a urlencoder encoded and" +a);
String B = Urlencoder.encode ("A In", "UTF-8");
LOG.I ("Encode", "a" urlencoder encoded and "+b");
String decode = Nurldecoder.decode ("A%e4%b8%ad", "UTF-8");
LOG.I ("Encode", "A%e4%b8%ad urldecoder Decoded"--"+decode");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
Operation Result:
10-17 07:33:14.357 1012-1012/com.xqx.encrypsthow i/encode:a urlencoder encoded-->a
Urlencoder encoded in 10-17 07:33:14.367 1012-1012/com.xqx.encrypsthow i/encode:a-->a%e4%b8%ad
10-17 07:33:14.377 1012-1012/com.xqx.encrypsthow i/encode:a%e4%b8%ad urldecoder decoding-->a
You can see that the character ' a ' is not encoded and Chinese ' in ' is encoded
corresponding to the Urlencoder encoding is the Urldecoder decoding
Can see "a"----------------"a"
Talking about Urlencoder coding algorithm