C#中文轉Unicode、Unicode轉中文及與js對應關係

中文轉Unicode:HttpUtility.UrlEncodeUnicode(string str); 
轉換後中文格式:"%uxxxx"  舉例:"柳_abc123"  轉換結果是:"%u67f3_abc123"

Unicode轉中文1:HttpUtility.UrlDecode(string str);
str格式:"%uxxxx" ,舉例:"%u67f3_abc123"

Unicode轉中文2:Regex.Unescape(string str);
str格式:"\uxxxx" ,舉例:"\u67f3_abc123"


參考資料:http://hi.baidu.com/eegcn/blog/item/7315f799a2e57f136f068c55.html

1.window.escape()與HttpUtility.UrlEncodeUnicode()編碼格式一樣:將一個漢字編碼爲%uxxxx格式
不會被window.escape編碼的字符有:@ _ - . * / +  這與http://www.w3school.com.cn/js/jsref_escape.asp上的解釋不符合

2.window.encodeURIComponent()與HttpUtility.UrlEncode()編碼格式一樣:將一個漢字編碼爲%xx%xx%xx的格式

不會被window.encodeURIComponent編碼的字符有:'()*-._!~ 這與http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp解釋相符合

不會被HttpUtility.UrlEncode編碼的字符有:'()*-._!相比較而言,HttpUtility.UrlEncode比window.encodeURIComponent多一個 ~ 編碼

 

3.不會被window.encodeURI編碼的字符有:-_.!*();/?:@&=$,# 與encodeURIComponent對比,發現encodeURI不對:;/?:@&=+$,#這些用於分隔 URI 組件的標點符號進行編碼

 

Asp.Net編碼與JS編碼的區別:

1. 不會被HttpUtility.UrlEncodeUnicode編碼的字符與不會被HttpUtility.UrlEncode編碼的字符一樣,而escape和encodeURIComponent不編碼的字符不一樣

2. HttpUtility.UrlEncode和HttpUtility.UrlEncodeUnicode會對/編碼,而escape和encodeURIComponent會對/編碼,encodeURI不會對/編碼

3. HttpUtility.UrlEncode()和HttpUtility.UrlEncodeUnicode()會把空格編碼爲 +,而escape,encodeURIComponent,encodeURI都會將空格編碼爲%20

 


使用ajax提交一個字符串:
1. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   var postStr="val={name:'梅小偉',age:19}";
   xmlHttp.send(postStr);   

客戶端發送請求如下:
POST /index.aspx HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://localhost.:3910/Default.aspx
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; baiduie8)
Host: localhost.:3910
Content-Length: 29
Connection: Keep-Alive
Pragma: no-cache

val={name:'梅小偉',age:19}//發現這裏沒有經過編碼,直接以2進制方式發送

在服務端index.aspx中打斷點,發現Request.Form爲:val=%7bname%3a'%u6885%u5c0f%u4f1f'%2cage%3a19%7d(這裏使用了escape編碼)使用Request.Form[0]取出的值和使用Request.Form["val"]取出的都爲“{name:'梅小偉',age:19}”

2. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   var postStr=window.encodeURIComponent("val={name:'梅小偉',age:19}");
   xmlHttp.send(postStr);

客戶端發送請求如下:
POST /index.aspx HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://localhost.:3910/Default.aspx
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; baiduie8)
Host: localhost.:3910
Content-Length: 59
Connection: Keep-Alive
Pragma: no-cache

val%3D%7Bname%3A'%E6%A2%85%E5%B0%8F%E4%BC%9F'%2Cage%3A19%7D//發現這裏使用了window.encodeURIComponent加碼

在服務端index.aspx中打斷點,發現Request.Form爲:val%3d%7bname%3a'%u6885%u5c0f%u4f1f'%2cage%3a19%7d(這裏居然使用了escape編碼,而不是encodeURIComponent編碼),使用Request.Form[0]取出的值爲“val={name:'梅小偉',age:19}”,使用Request.Form["val"]取出的值爲null(這是因爲客戶端發送請求時將=編碼爲%3d了,如果使用window.encodeURI這裏就能取出Request.Form["val"]爲:“{name:'梅小偉',age:19}”了)

 

總結:不是使用get或者post,只要都是使用form的enctype屬性的默認值application/x-www-form-urlencoded,所以如果你要傳的值都會經過window.encodeURIComponent()編碼再傳送(除了值包含空格不會被編碼爲%20,而是編碼爲+).傳到服務器後,可以用Server.UrlDecode()進行解碼。但是要注意,不管是get方式還是post方式,enctype爲application/x-www-form-urlencoded還是multipart/form-data,用asp.net在後臺查看Request.QueryString和Request.Form的時候,中文又變成了escape編碼格式,例如Request.Form=__VIEWSTATE=%2fwEPDwUJNzgzNDMwNTMzZGSvF5y%2bl0lztppRS7QNr4qmrF4KTw%3d%3d&mm=%u6556%u5fb7%u8428%u7684(英語字母不會被編碼,而一些符號使用encodeURIComponent和escape編碼後相同,如=,$等等)。


爲什麼優先使用encodeURIComponent而不是escape?
escape方 法並不編碼字符+。而我們知道,在用戶提交的表單字段中,如果有空格,則會被轉化爲+字符,而服務器解析的時候則會認爲+號代表空格。由於這個缺 陷,escape方法並不能正確地處理所有的非ASCII字符,你應當儘量避免使用escape方法,取而代之,你最好選擇 encodeURIComponent()方法。


 

同時有位仁兄在http://hi.baidu.com/zkbob22/blog/item/9159cf96768b587955fb9684.html留言裏提到:

Unicode到中文: HttpUtility.UrlDecode、Regex.Unescape、Encoding.Unicode.GetString(Encoding.Unicode.GetBytes())、Encoding.Convert
中文到Unicode:HttpUtility.UrlEncodeUnicode、Encoding.Convert


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章