Summary of JavaScript and C # URL encoding and decoding,
JavaScript Section
EncodeURI () (decoded as: decodeURI (): Escape Character :-_.! ~ *'();/? : @ & =+ $ ,#
For example: encodeURI ("http://www.bkjia.com? A = -_.!~ *'();/? : @ & =+ $, # ") Output:" http://www.bkjia.com? A = -_.!~ *'();/? : @ & =+ $ ,#"
EncodeURIComponent () (decoded as: decodeURIComponent (): Escape Character :-_.! ~ *'()
For example:
EncodeURIComponent ("http://www.bkjia.com? A = -_.!~ *'();/? : @ & =+ $ ,#")
Output:
"Http % 3A % 2F % 2Fwww.jb51.net % 3Fa % 3D -_.!~ * '() % 3B % 2F % 3F % 3A % 40% 3D % 2B % 26% 2C % 23"
Difference: ;/? : @ & =+ $, # Processing of these characters.
There is also an additional: escape (), but ECMAScript v3 is opposed to using this method. The cause is determined by the escape sequence. The transfer sequence of escape () is:
For replacement characters whose code unit is less than or equal to 0xFF, use the two-digit escape sequence in % xx format. For replacement characters whose code unit is greater than 0xFF, use the four-digit escape sequence in % uxxxx format.
For details, we can look at the ECMAScript specifications. Because I am too simple to do so, I will not study it in depth .. It is also not recommended to use unescape.
C # section:
The C # section is messy, with not only a lot of urlencode, but also a lot of htmlencode. But in fact, we only pay attention to the url part when processing the url. The html part is used only when processing the html part. For example, the htmlencode and htmldecode should be used to prevent xss attacks.
The following describes common url methods:
Server. UrlEncode (decoding: Server. UrlDecode ):
Server. urlEncode uses the system preset encoding as a parameter to call HttpUtility. urlEncode encoding, so if the system uses UTF8 encoding globally, the two methods are the same (whether the system preset encoding is used remains to be verified and no official statement is found ).
HttpUtility. UrlEncode (decoding: HttpUtility. UrlDecode)
For example:
HttpUtility. UrlEncode ("http://www.bkjia.com? A = -_.!~ *'();/? : @ & =+ $ ,#")
Output:
Http % 3a % 2f % 2fwww.jb51.net % 3fa % 3d -_.! % 7e * % 27 () % 3b % 2f % 3f % 3a % 40% 3d % 2b % 26% 2c % 23
Visible: This method encodes the url address. However, you need to know that this method will encode the space as the plus sign rather than the hexadecimal % 20. Therefore, if there is a space in the encoding parameter of this method, it will cause an error.
Uri. EscapeUriString (decoding: The corresponding string is not found): Escape Chinese characters and spaces.
For example:
Uri. EscapeUriString ("http://www.bkjia.com? A = China & 123 -_.!~ *'();/? : @ & =+ $ ,#")
Output:
Http://www.bkjia.com? A = % E4 % B8 % AD % E5 % 9B % BD & 123% 20% 20% 20 -_.!~ *'();/? : @ & =+ $ ,#
As you can see, it does not encode the URL.
Uri. EscapeDataString (decoding: Uri. UnescapeDataString): Not only parameters, but also URL encoding.
For example:
Uri. EscapeDataString ("http://www.bkjia.com? A = China & 123 -_.!~ *'();/? : @ & =+ $ ,#")
Output: http % 3A % 2F % 2Fwww.jb51.net % 3Fa % 3D % E4 % B8 % AD % E5 % 9B % BD % 26123% 20% 20% 20-_. % 21 ~ % 2A % 2
7% 28% 29% 3B % 2F % 3F % 3A % 40% 26% 3D % 2B % 24% 2C % 23
Therefore, to sum up:
No encoding required: // available: encodeURI is used in JavaScript; otherwise, encodeURIComponent is used; Uri. EscapeUriString is used in C #; otherwise, Uri. EscapeDataString is used.