Heim  >  Artikel  >  Web-Frontend  >  js中escape对应的C#解码函数 UrlDecode_基础知识

js中escape对应的C#解码函数 UrlDecode_基础知识

WBOY
WBOYOriginal
2016-05-16 17:46:231139Durchsuche

js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码
需要注意的几点:
1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。
2、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。
3、用HttpUtility.UrlEncode编码后的字符串和用Server.UrlEncode进行编码后的字符串对象不一样:
例如:

复制代码 代码如下:

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));

输出结果是
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%e5%a5%a5%e8%bf%b7%e5%b0%94
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
原因:Server.UrlEncode的编码方式是按照本地程序设置的编码方式进行编码的,而HttpUtility.UrlEncode是默认的按照.net的utf-8格式进行编码的。
如果改一下程序:
复制代码 代码如下:

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));

输出的结果是
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
3、有时候可能别的系统传递过来的url是用别的编码方式编码的。
介绍自己编写的一个方法,可以获取指定编码格式的QueryString。
复制代码 代码如下:

public string GetNonNullQueryString(string key,Encoding encoding)
{
//引用System.Collections.Specialized和System.Text命名空间
string stringValue;
System.Collections.Specialized.NameValueCollection encodingQueryString;
//该方法是在2.0中新增的
encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding);
//'里面的key就是你提交的参数的Key
return encodingQueryString[key] != null ? encodingQueryString[key].Trim() : "";
}

调用
string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();
----------------------------------------------------------------------------------------------

javascript中escape,encodeURI,encodeURIComponent三个函数的区别
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent
1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。
例如:
2、 进行url跳转时可以整体使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用数据时可以使用escape
[Huoho.Com编辑]
例如:搜藏中history纪录。
4、 escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn