成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

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

2011-10-17 11:09:37來源:柳永法作者:

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

中文轉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://www.yongfa365.com/Item/Unicode-Chinese-UrlEncodeUnicode-Unescape.html

關鍵詞:C#Unicode中文
主站蜘蛛池模板: 理塘县| 大城县| 芒康县| 永和县| 南康市| 琼海市| 德州市| 余江县| 陇西县| 依兰县| 镇远县| 裕民县| 紫金县| 漳州市| 三河市| 南溪县| 山丹县| 赣榆县| 阳泉市| 华蓥市| 云浮市| 巢湖市| 三亚市| 广汉市| 会宁县| 涿州市| 华亭县| 蒙山县| 武平县| 治多县| 九寨沟县| 北碚区| 牙克石市| 盐亭县| 漠河县| 分宜县| 兰溪市| 吉林市| 吉隆县| 双峰县| 开封市|