JavaScript and C # URI Encoding

Source: Internet
Author: User
Tags urlencode
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.

There are 69 unencoded characters in escape:*, +,-,.,/, @, _, 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:

encodeURI("http://www.cnblogs.com/a file with spaces.html")
// outputs http://www.cnblogs.com/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:

VaR Param = "blog ";
VaR url = "http://www.cnblogs.com /? Key = "+ encodeuricomponent (PARAM) +" & page = 1 ";
Console. Log (URL); // outputs http://www.cnblogs.com /? 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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (httputility. urlencode (url1 ));

// Output
Http%3a%2f%2fwww.cnblogs.com%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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (URI. escapeuristring (url1 ));
// Outputs:
Http://www.cnblogs.com/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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (URI. escapedatastring (url1 ));
// Outputs:
Http%3a%2f%2fwww.cnblogs.com%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.

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.

There are 69 unencoded characters in escape:*, +,-,.,/, @, _, 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:

encodeURI("http://www.cnblogs.com/a file with spaces.html")
// outputs http://www.cnblogs.com/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:

VaR Param = "blog ";
VaR url = "http://www.cnblogs.com /? Key = "+ encodeuricomponent (PARAM) +" & page = 1 ";
Console. Log (URL); // outputs http://www.cnblogs.com /? 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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (httputility. urlencode (url1 ));

// Output
Http%3a%2f%2fwww.cnblogs.com%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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (URI. escapeuristring (url1 ));
// Outputs:
Http://www.cnblogs.com/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:

String url1 = "http://www.cnblogs.com/a file with spaces.html? A = 1 & B = blog park # ABC ";
Response. Write (URI. escapedatastring (url1 ));
// Outputs:
Http%3a%2f%2fwww.cnblogs.com%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.

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.