C # decoding function System. Web. HttpUtility. UrlDecode (s) corresponding to escape in js // pay attention to Encoding
Notes:
1. HttpUtility. UrlEncode and HttpUtility. UrlDecode are static methods, while Server. UrlEncode and Server. UrlDecode are instance methods.
2. Server is an instance of the HttpServerUtility class and a property of System. Web. UI. Page.
3. the string encoded with HttpUtility. UrlEncode is different from the string object encoded with Server. UrlEncode:
For example:
Copy codeThe Code is as follows:
String url = "http://search.99read.com/index.aspx? Book_search = all & main_str = omyr ";
Response. Write (HttpUtility. UrlEncode (url ));
Response. Write ("<br> ");
Response. Write (Server. UrlEncode (url ));
The output result is:
Http % 3a % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 dall % 26main_str % 3d % e5 % a5 % a5 % e8 % bf % b7 % e5 % b0 % 94
Http % 3a % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 dall % 26main_str % 3d % b0 % c2 % c3 % d4 % b6 % fb
Cause: Server. UrlEncode is encoded according to the encoding method set by the local program, while HttpUtility. UrlEncode is encoded in. net UTF-8 format by default.
If you change the program:
Copy codeThe Code is as follows:
String url1 = "http://search.99read.com/index.aspx? Book_search = all & main_str = omyr ";
Response. Write (HttpUtility. UrlEncode (url1, System. Text. Encoding. GetEncoding ("GB2312 ")));
Response. Write ("<br> ");
Response. Write (Server. UrlEncode (url1 ));
The output result is:
Http % 3a % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 dall % 26main_str % 3d % b0 % c2 % c3 % d4 % b6 % fb
Http % 3a % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 dall % 26main_str % 3d % b0 % c2 % c3 % d4 % b6 % fb
3. Sometimes the URLs transmitted by other systems are encoded in other encoding methods.
This section describes a self-written method to obtain the QueryString of the specified encoding format.
Copy codeThe Code is as follows:
Public string GetNonNullQueryString (string key, Encoding encoding)
{
// Reference the System. Collections. Specialized and System. Text namespaces.
String stringValue;
System. Collections. Specialized. NameValueCollection encodingQueryString;
// This method is added in 2.0.
EncodingQueryString = HttpUtility. ParseQueryString (Request. Url. Query, encoding );
// The key in 'is the Key of the parameter you submitted
Return encodingQueryString [key]! = Null? EncodingQueryString [key]. Trim ():"";
}
Call:
String url = GetNonNullQueryString ("url", Encoding. UTF8). Trim ();
Bytes ----------------------------------------------------------------------------------------------
Differences between escape, encodeURI, and encodeURIComponent functions in javascript
JavaScript code involves three functions: escape, encodeURI, and encodeURIComponent. The corresponding three decoding functions are: unescape, decodeURI, and decodeURIComponent.
1. encodeURIComponent is required when passing parameters so that the combined url will not be truncated by special characters such.
Example: <script language = "javascript"> document. write ('<a href = "http://passport.baidu.com /? Logout & aid = 7 & u = '+ encodeURIComponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </script>
2. encodeURI can be used for url redirection.
Example: Location. href = encodeURI ("http://cang.baidu.com/do/s? Word = Baidu & ct = 21 ");
3. You can use escape when js uses data.
[Edit Huoho. Com]
For example, search for the history record in the bucket.
4. When encoding unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.
The most commonly used format is encodeURIComponent, which converts special characters such as Chinese and Korean into UTF-8 url encoding, therefore, if you want to use encodeURIComponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)
Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z
EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z
EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z