Summary of JavaScript C and URL encoding and decoding

  • 2021-07-13 04:15:16
  • OfStack

JavaScript Part

encodeURI () (decoded as: decodeURI ()): Character that will not be escaped: -_.! ~ * '();/?: @ & =+$,#


 For example: 
encodeURI( " https://www.ofstack.com?a=-_.!~*'();/?:@&=+$,# " )
 Output: 
 " https://www.ofstack.com?a=-_.!~*'();/?:@&=+$,# " 

encodeURIComponent () (decoded as: decodeURIComponent ()): Character that will not be escaped: _.! ~ * '()

For example:

encodeURIComponent ("https://www. ofstack. com? a=-_.! ~ * '();/?: @ & = + $, # ")

Output:

"http% 3A% 2F% 2Fwww. ofstack. com% 3Fa% 3D-_.! ~ * '()% 3B% 2F% 3F% 3A% 40% 26% 3D% 2B% 24% 2C% 23"

Difference: for; /? : @ & = + $, # The processing of these characters.

There is an additional: escape (), but ECMAScript v 3 opposes using this method. The reason guess is related to the escape sequence, and the transition sequence of escape () is:
For substituted characters with code units less than or equal to 0xFF, use a two-digit escape sequence of% xx format. For substituted characters with code units greater than 0xFF, use a 4-digit escape sequence of% uxxxx format.
Specific can study 1 under the ECMAScript specification, because I do too shallow, not in-depth study, ha. . unescape () is also not recommended.

C # Section:

The C # section is confusing, not only a lot of urlencode, but also a lot of htmlencode. But in fact, when dealing with url, we only pay attention to url, and html will only be used when dealing with html. For example, htmlencode and htmldecode should be needed to prevent xss attacks.

Let's talk about the common methods of url:

Server. UrlEncode (decoding: Server. UrlDecode):
Server. UrlEncode uses the system preset code as a parameter to call HttpUtility. UrlEncode code, so if the whole system is encoded in UTF8 format, these two methods are the same (as to whether the system preset code is used or not, it remains to be verified, and no official statement has been found).
HttpUtility. UrlEncode (decoding: HttpUtility. UrlDecode)

For example:

HttpUtility. UrlEncode ("https://www. ofstack. com? a=-_.! ~ * '();/?: @ & = + $, # ")

Output:

http%3a%2f%2fwww.ofstack.com%3fa%3d-_.!%7e*%27()%3b%2f%3f%3a%40%26%3d%2b%24%2c%23

It can be seen that this method encodes the url address. One thing to know, however, is that this method encodes spaces as + instead of% 20 in 106, so it will cause an error if there is a space in the encoding parameter of this method.

Uri. EscapeUriString (decoding: no corresponding found): Chinese, spaces, etc. will be escaped.

For example:

Uri. EscapeUriString ("https://www. ofstack. com? a = China & 123 -_.!~*'();/?:@ & = + $, # ")

Output:

https://www.ofstack.com?a=%E4%B8%AD%E5%9B%BD & 123%20%20%20-_.!~*'();/?:@ & =+$,#

It can be seen that it does not encode the URL.

Uri. EscapeDataString (decoding: Uri. UnescapeDataString): Not only the parameters, but also the URL is encoded.

For example:

Uri. EscapeDataString ("https://www. ofstack. com? a= China & 123 -_.!~*'();/?:@ & = + $, # ")

Output: http% 3A% 2F% 2Fwww. ofstack. com% 3Fa% 3D% E 4% B 8% AD% E 5% 9B% BD% 26123% 20% 20% 20-_.% 21 ~% 2A% 2
7%28%29%3B%2F%3F%3A%40%26%3D%2B%24%2C%23

So, to sum up:

No coding required://Available: encodeURI in JavaScript, otherwise encodeURIComponent, Uri. EscapeUriString in C # or Uri. EscapeDataString


Related articles: