Home  >  Article  >  Web Front-end  >  C# decoding function corresponding to escape in js UrlDecode_Basic knowledge

C# decoding function corresponding to escape in js UrlDecode_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:46:231137browse

The C# decoding function corresponding to escape in js System.Web.HttpUtility.UrlDecode(s) //Pay attention to encoding
Several points to note:
1. HttpUtility.UrlEncode, HttpUtility.UrlDecode are static methods, and Server. UrlEncode, 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 Code The code is as follows:

string url="http://search.99read.com/index.aspx?book_search=all&main_str=欧米尔";
Response.Write(HttpUtility.UrlEncode(url));
Response.Write("
");
Response.Write(Server.UrlEncode(url));

The output result is :
http://search.99read.com/index.aspx?book_search=all&main_str=AOMIER
http://search.99read.com/ index.aspx?book_search=all&main_str=���GG�
Cause: The encoding method of Server.UrlEncode is encoded according to the encoding method set by the local program, while HttpUtility.UrlEncode is the default encoding method. Encoded in .net UTF-8 format.
If you change the program:
Copy the code The code is as follows:

string url1=" http://search.99read.com/index.aspx?book_search=all&main_str=欧米尔";
Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312"))) ;
Response.Write("
");
Response.Write(Server.UrlEncode(url1));

The output result is :
http://search.99read.com/index.aspx?book_search=all&main_str=���GG�
http://search.99read.com/index.aspx?book_search=all&main_str=�� �ն�
3. Sometimes the URL passed by other systems may be encoded using other encoding methods.
Introducing a method written by myself to obtain the QueryString in the specified encoding format.
Copy code The code is as follows:

public string GetNonNullQueryString(string key,Encoding encoding)
{
//Reference System.Collections.Specialized and System.Text namespace
string stringValue;
System.Collections.Specialized.NameValueCollection encodingQueryString;
//This method is new in 2.0
encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding);
//'The key inside is the Key of the parameter you submitted
return encodingQueryString[key] != null ? encodingQueryString[key ].Trim() : "";
}

Call :
string url = GetNonNullQueryString("url",Encoding.UTF8).Trim() ;
------------------------------------------------ ---------------------------------------------

The difference between escape, encodeURI, and encodeURIComponent in JavaScript
JS encoding text involves 3 functions: escape, encodeURI, encodeURIComponent, and the corresponding 3 decoding functions: unescape, decodeURI, decodeURIComponent
1 , You need to use encodeURIComponent when passing parameters, so that the combined URL will not be truncated by special characters such as #.
For example:
2. You can use encodeURI as a whole when performing url jump
For example: Location.href=encodeURI ("http://cang.baidu.com/do/s?word=Baidu&ct=21");
3. You can use escape when using data in js
[Huoho.Com editor]
For example : History record in collection.
4. When escape encodes unicode values ​​other than 0-255, it outputs the %u**** format. In other cases, the encoding results of escape, encodeURI, and encodeURIComponent are the same.
The most commonly used one should be encodeURIComponent, which converts Chinese, Korean and other special characters into url encoding in utf-8 format, so if you need to use encodeURIComponent to pass parameters to the background, you need background decoding to support utf-8 (in the form The encoding method is the same as the encoding method of the current page)
There are 69 characters that are not encoded by escape: *, , -, ., /, @, _, 0-9, a-z, A-Z
There are 82 characters that are not encoded by encodeURI Numbers:!,#,$,&,',(,),*, ,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent does not encode 71 characters: !, ', (,), *, -, ., _, ~, 0-9, a-z, A-Z
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn