首頁  >  文章  >  web前端  >  js中escape對應的C#解碼函數 UrlDecode_基礎知識

js中escape對應的C#解碼函數 UrlDecode_基礎知識

WBOY
WBOY原創
2016-05-16 17:46:231139瀏覽

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.Url Response.Write(Server.Url





輸出結果是: http://search.99read.com/index.aspx?book_search=all&main_str=奧迷爾http://search.99read.com/index .aspx?book_search=all&main_str=���信息�
原因
:Server.UrlEncode的編碼方式是按照本地程式設定的編碼方式進行編碼的,而HttpUtility.UrlEncode是預設的按照. net的utf-8格式進行編碼的。
如果改一下程式:



複製程式碼
程式碼如下:


stringurl 1=" 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://search.99read.com/index.aspx?book_search=all&main_str=���六位工作 -
http://search.99read.com/index.aspx?book_search=all&main_str=�� 六翼�
3、有時候可能別的系統傳遞過來的url是用別的編碼方式編碼的。
介紹自己寫的一個方法,可以取得指定編碼格式的QueryString。




複製程式碼


程式碼如下:

public string GetNonNullQueryString(string ,EncoEncod >{
//引用System.Collections.Specialized和System.Text命名空間string stringValue; System.Collections.Specialized.NameValueCollection encodingQueryString;
//該方法新增是在2.00的
encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding);
//'裡面的key就是你提交的參數的Key
return encodingQueryString[key] != nulleryString> ].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
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn