confusing URI encoding
There are three ways to encode in javascript: Escape, encodeURI, encodeURIComponent
Main methods of encoding in C #: Httputility.urlencode, Server.URLEncode, uri.escapeuristring, uri.escapedatastring
It's okay in JavaScript, only provided three, C # is mainly used in so many, there is no other coding (HTML), a lot of do not understand, do not understand the heart of fear, the heart of fear becomes bitter force, this article to explain in detail in JavaScript and C # Method of how to encode a URI (note: No other encodings are involved in this article).
escape: Not recommended
Reason: Eacape is a method in the BOM that only encodes the ASCII symbol correctly, while encodeURI, encodeuricomponent can encode all Unicode symbols. ECMAScript v3 against using this method, the application uses decodeURI () and decodeuricomponent () to replace it.
Escape does not encode 69 characters: *,+,-,.,/,@,_,0-9,a-z,a-z
encodeURI: Used to encode URLs (no parameters included)
There are 82 encodeURI not encoded characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z
encodeURI is designed for this. encodeURI does not encode special characters in the URI, such as a colon (:), slash (/). Let's look at an example:
Copy Code code as follows:
encodeURI ("http://www.jb51.net/a file with spaces.html")
Outputs http://www.jb51.net/a%20file%20with%20spaces.html
You can see that the space is replaced by just 20%, so this method can be used to encode the URL.
Because encodeURI does not encode a colon (:), slash (/), an error is resolved if the parameter (such as the URL as an argument) contains a colon (:), slash (/), so this method cannot encode the parameter.
encodeuricomponent: Used to encode URL parameters
There are 71 encodeuricomponent characters that are not encoded:!, ', (,), *,-,.,_,~,0-9,a-z,a-z
You can see that this method encodes:/All, so you cannot use it to encode URLs. Because this method encodes in Chinese, white space, pound sign (#), slash (/), colon (:) are encoded, it is appropriate to encode the parameters in the URI. Look at the following example:
Copy Code code as follows:
var param= "blog Park";
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 (this is coded only for the parameters that need to be encoded (Page=1 does not require encoding).
Server.URLEncode && Httputility.urlencode: not recommended
Put the two together because the two methods are the same in most cases. The difference is that Httputility.urlencode by default uses the UTF8 format encoding, and Server.URLEncode is encoded using the System preset format, server.urlencode using a systematic preset encoding as a parameter call Httputility.urlencode Code, so if the system is globally all encoded in UTF8 format, the two methods are the same.
How are these two methods coded, let's look at an example:
Copy Code code as follows:
String url1 = "http://www.jb51.net/a file with spaces.html?a=1&b= blog Garden #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 we can see from the above example, the Httputility.urlencode encodes the colon (:) and slash (/), so it cannot be used to encode the URL.
So can you encode the parameters, the answer is no. Since the parameters of the hollow lattice should be encoded as%20 instead of being encoded as a plus sign (+) by Httputility.urlencode, these two methods are not recommended for encoding the URI.
uri.escapeuristring: Used to encode URLs (no parameters included)
We still use the example to say:
Copy Code code as follows:
String url1 = "http://www.jb51.net/a file with spaces.html?a=1&b= blog Garden #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
As you can see, the uri.escapeuristring encodes the spaces and encodes the Chinese, but does not encode the colon (:), slash (/) and pound sign (#), so this method can be used to encode the URL, but not to encode the parameters. Acts like a encodeURI method in JavaScript.
uri.escapedatastring: Used to encode URL parameters
Still use the example to speak:
Copy Code code as follows:
String url1 = "http://www.jb51.net/a file with spaces.html?a=1&b= blog Garden #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
As you can see, uri.escapedatastring the colon (:), slash (/), space, Chinese, pound (#) code, so this method can not be used for URL encoding, but can be used to encode parameters, Acts like a encodeURIComponent method in JavaScript.
Summary
The recommended practice in JavaScript is to encode the URL part of the URI using encodeURI, and to encode the parameters passed in the URI with encodeURIComponent.
The recommended practice in C # is to encode the URL portion of the URI using uri.escapeuristring, and to encode the parameters passed in the URI with uri.escapedatastring.
The decoding part does not say, corresponds with the coding square method.
Author: Tian Xing Jian, self-improvement
Source: http://artwl.cnblogs.com