I am no longer afraid of URI encoding JavaScript and C # URI Encoding

Source: Internet
Author: User

Chaotic URI Encoding
There are three methods for encoding in JavaScript: escape, encodeURI, encodeURIComponent

C # encoding methods: HttpUtility. UrlEncode, Server. UrlEncode, Uri. EscapeUriString, Uri. EscapeDataString

The JavaScript code is okay. Only three codes are provided, so many codes are used in C # and other codes (HTML) are not listed yet. If there are more than one code, you cannot understand it, if you don't understand it, you will feel fear, and the fear will become hard. This article will explain in detail how to encode URI in JavaScript and C # (note: this article does not involve other encodings ).

Escape: Not recommended
Reason: eacape is a BOM method and can only be correctly encoded with ASCII characters, while encodeURI and encodeURIComponent can be encoded with all Unicode characters. ECMAScript v3 opposed this method. The application uses decodeURI () and decodeURIComponent () to replace it.

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z

EncodeURI: used for URL encoding (excluding parameters)
EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z

EncodeURI is designed for this purpose. EncodeURI does not encode special characters in URI, such as colon (:) and slash (/). The following is an example:Copy codeThe Code is as follows: encodeURI ("http://www.jb51.net/a file with spaces.html ")
// Outputs http://www.jb51.net/a%20file%20with%20spaces.html

We can see that the space is replaced with 20%, so this method can be used to encode the URL.

Because encodeURI does not encode colons (:) or slashes (/), if the parameter (such as the URL as a parameter) contains colons (:) or slashes (/), an error will be resolved, therefore, this method cannot encode parameters.

EncodeURIComponent: used to encode URL parameters
EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z

We can see that this method is encoded for:/, so it cannot be used to encode the URL. This method is suitable for encoding parameters in URI because the Chinese characters, spaces, pound signs (#), diagonal lines (/), and colons (:) are all encoded. See the following example:Copy codeThe Code is as follows: var param = "";
Var url = "http://www.jb51.net /? Key = "+ encodeURIComponent (param) +" & page = 1 ";
Console. log (url); // outputs http://www.jb51.net /? Key = % E5 % 8D % 9A % E5 % AE % A2 % E5 % 9B % AD & page = 1

As you can see, this is exactly the result we want (only the encoded parameter (page = 1 does not need to be encoded ).

Server. UrlEncode & HttpUtility. UrlEncode: Not recommended
Put the two together because the two methods are the same in most cases. Their difference is HttpUtility. urlEncode uses UTF-8 encoding by default, while Server. urlEncode uses the system preset format encoding, Server. urlEncode uses the system preset encoding as a parameter to call HttpUtility. urlEncode encoding, so if the system uses UTF8 format globally, the two methods are the same.

How are these two methods encoded? Let's look at an example:Copy codeThe Code is as follows: string url1 = "http://www.jb51.net/a file with spaces.html? A = 1 & B = blog park # abc ";
Response. Write (HttpUtility. UrlEncode (url1 ));

// Output
Http%3a%2f%2fwww.jb51.net%2fa%file%with%spaces.html % 3fa % 3d1% 26b % 3d % e5 % 8d % 9a % e5 % AE % a2 % e5 % 9b % ad % 23abc

As shown in the preceding example, HttpUtility. UrlEncode encodes the colon (:) and slash (/), so it cannot be used to encode the URL.

So can we encode the parameters? The answer is no. Because the space in the parameter should be encoded as % 20 rather than HttpUtility. UrlEncode as the plus sign (+), we do not recommend using these two methods to encode the URI.

Uri. EscapeUriString: used for URL encoding (excluding parameters)
Let's talk about it using examples:Copy codeThe Code is as follows: string url1 = "http://www.jb51.net/a file with spaces.html? A = 1 & B = blog park # abc ";
Response. Write (Uri. EscapeUriString (url1 ));
// Outputs:
Http://www.jb51.net/a%20file%20with%20spaces.html? A = 1 & B = % E5 % 8D % 9A % E5 % AE % A2 % E5 % 9B % AD # abc

We can see that Uri. the EscapeUriString is encoded with spaces and Chinese characters, but the colon (:), slash (/), and well (#) are not encoded, therefore, this method can be used for URL encoding, but cannot encode parameters. It is similar to the encodeURI method in JavaScript.

Uri. EscapeDataString: used to encode URL parameters.
Still use the example:Copy codeThe Code is as follows: string url1 = "http://www.jb51.net/a file with spaces.html? A = 1 & B = blog park # abc ";
Response. Write (Uri. EscapeDataString (url1 ));
// Outputs:
Http%3A%2F%2Fwww.jb51.net%2Fa%20file%20with%20spaces.html % 3Fa % 3D1% 26b % 3D % E5 % 8D % 9A % E5 % AE % A2 % E5 % 9B % AD % 23abc

We can see that Uri. escapeDataString encode the colon (:), slash (/), space, Chinese characters, and pound (#). Therefore, this method cannot be used for URL encoding, however, it can be used to encode parameters, similar to the encodeURIComponent method in JavaScript.

Summary
In JavaScript, we recommend that you use encodeURI to partially encode the uri url and encodeURIComponent to encode the parameters passed in the URI.

In C #, we recommend that you use Uri. EscapeUriString to encode the URL of the URI. Use Uri. EscapeDataString to encode the parameters passed in the URI.

The decoding part will not be mentioned, which corresponds to the encoding method.
Author: tianxingjian, self-improvement

Source: http://artwl.cnblogs.com

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.