C # decoding function UrlDecode _ basic knowledge of escape in js

Source: Internet
Author: User
The C # decoding function System corresponding to escape in js. web. httpUtility. urlDecode (s). Pay attention to the C # decoding function System corresponding to escape in js as follows. web. httpUtility. urlDecode (s) // 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:

The 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 ("
");
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:

The 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 ("
");
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.

The 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.
For example:

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.