JS編碼和Asp.net編碼

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

 

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

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

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

 

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

 

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

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

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

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

客戶端發送請求以下:
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()方法。

相關文章
相關標籤/搜索