Talking about Urlencoder coding algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.