Principle analysis of twice encodeURI and URLDecode

When using the address bar to submit query parameters, if not encoded, non-English characters will be encoded according to the character set of the operating system and submitted to the server, and the server will decode it according to the configured character set, so if the two are inconsistent, it will lead to garbled characters. If the encodeURI is only performed once, because the encodeURI function uses UTF-8 to encode the URL, if the server uses other encoding methods when decoding, garbled characters will appear. The default decoding character set configured by the server is not UTF-8. , so in most cases, garbled characters will be generated when submitting Chinese query parameters in the address bar; in this case, you can use encodeURI twice in a row to encode non-English characters on the client (mainly the browser), and then use java on the server .net.URLDecoder(String."UTF-8") decoding, you can get the correct Chinese.

 

principle:

The .encodeURL function is mainly used to transcode the URI. It uses the UTF-8 encoding by default.
. UTF-8 encoding format: a Chinese character is composed of three bytes, and each byte will be converted into 16 bytes. system code, and add a % sign.

Assuming that the Chinese input on the page side is a "中", follow the steps below to decode

1. For the first time encodeURI, obtain the byte array according to the utf-8 method and turn it into [-28,-72-83], traverse the bytecode array, and convert each byte into the corresponding hexadecimal number, This becomes [E4,B8,AD], and finally becomes [%E4,%B8,%AD] At this point, there are no multi-byte characters, all are single-byte characters.

2. The second encodeURI, when encoding, will treat % as an escape character, and will not encode characters after %, and will encode % into %25. The array will finally become [%25E4,%25B8,%25AD] Then send the processed data [%25E4,%25B8,%25AD] to the server side.
When the application server calls the getParameter method, the getParameter method will go to the application server to request parameters.
The application server initially obtains the sent [%25E4, %25B8,%25AD], the application server will perform URLdecode operation on this data, this time when the application server decodes, whether it is according to UTF-8, GBK, or ISO-8859, you can get [%E4,%B8 ,%AD], because %25 will be parsed into %. And this value will be returned to the getParameter method

3\, decode it with UTF-8 again, and get "Medium".

Guess you like

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