標籤:val .com javascrip 字元 gb2312 script 引用 port search
js中escape對應的C#解碼函數 System.Web.HttpUtility.UrlDecode(s),使用過程中有以下幾點需要注意 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("<br>"); 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("<br>"); 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才不會被#等特殊字元截斷。
例如:
<script language="javascript">
document.write(‘<a href="http://passport.baidu.com/?logout&aid=7&u=‘+encodeURIComponent("http://cang.baidu.com/bruce42")+‘">退出</a>‘);
</script>
2、 進行url跳轉時可以整體使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用資料時可以使用escape
例如:搜藏中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
js中escape對應的C#解碼函數 UrlDecode