中文轉Unicode:HttpUtility.UrlEncodeUnicode(string str);html
轉換後中文格式:"%uxxxx"
舉例:"柳_abc123" java轉換結果是:"%u67f3_abc123" Unicode轉中文1:HttpUtility.UrlDecode(string str);jquery
str格式:"%uxxxx"
,舉例:"%u67f3_abc123" 程序員str格式:"\uxxxx"Unicode轉中文2:Regex.(string str);ajax
,舉例:"\u67f3_abc123"
1.window.escape()與HttpUtility.UrlEncodeUnicode()編碼格式同樣:將一個漢字編碼爲%uxxxx格式服務器
不會被window.escape編碼的字符有:@ _ - . * / +
這與http://www.w3school.com.cn/js/jsref_escape.asp上的解釋不符合 app不會被window.encodeURIComponent編碼的字符有:'()*-._!~ 這與 http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp2.window.encodeURIComponent()與HttpUtility.UrlEncode()編碼格式同樣:將一個漢字編碼爲%xx%xx%xx的格式asp.net
解釋相符合jsp
不會被HttpUtility.UrlEncode編碼的字符有:'()*-._!相比較而言,HttpUtility.UrlEncode比window.encodeURIComponent多一個 ~ 編碼 ide
3.不會被window.encodeURI編碼的字符有:-_.!*();/?:@&=$,# 與encodeURIComponent對比,發現encodeURI不對:;/?:@&=+$,#這些用於分隔 URI 組件的標點符號進行編碼
1. 不會被HttpUtility.UrlEncodeUnicode編碼的字符與不會被HttpUtility.UrlEncode編碼的字符同樣,而escape和encodeURIComponentAsp.Net編碼與JS編碼的區別:
不編碼的字符不同
2. HttpUtility.UrlEncode和HttpUtility.UrlEncodeUnicode會對/編碼,而escape和encodeURIComponent會對/編碼,encodeURI不會對/編碼
3. HttpUtility.UrlEncode()和HttpUtility.UrlEncodeUnicode()會把空格編碼爲 +,而escape,encodeURIComponent,encodeURI都會將空格編碼爲
使用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
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727;Accept-Encoding: gzip, deflate
.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
在服務端index.aspx中打斷點,發現Request.Form爲:val={name:'%u6885%u5c0f%u4f1f',age:19}(這裏竟然使用了escape編碼,val={name:'梅小偉',age:19}//發現這裏使用了window.encodeURIComponent加碼
而不是encodeURIComponent編碼),使用Request.Form[0]取出的值爲「val={name:'梅小偉',age:19}」,使用Request.Form["val"]
取出的值爲null(這是由於客戶端發送請求時將=編碼爲=了,若是使用window.encodeURI這裏就能取出Request.Form["val"]爲:「{name:'梅小偉',age:19}」了)
總結:不是使用get或者post,只要都是使用form的enctype屬性的默認值application/x-www-form-urlencoded,因此若是你要傳
的值都會通過window.encodeURIComponent()編碼再傳送(除了值包含空格不會被編碼爲 ,而是編碼爲+).傳到服務器後,能夠
用Server.UrlDecode()進行解碼。可是要注意,不論是get方式仍是post方式,enctype爲application/x-www-form-urlencoded還
是multipart/form-data,用asp.net在後臺查看Request.QueryString和Request.Form的時候,中文又變成了escape編碼格式,例
如Request.Form=__VIEWSTATE=/wEPDwUJNzgzNDMwNTMzZGSvFy+l0lztppRS7QNr4qmrF4KTw==&mm=%u6556%u5fb7%u8428%u7684(英語字母不會被編碼,而一些符號使用encodeURIComponent和escape編碼後相同,如=,$等等)。
escape方 法並不編碼字符+。而咱們知道,在用戶提交的表單字段中,若是有空格,則會被轉化爲+字符,而服務器解析的時候則爲何優先使用encodeURIComponent而不是escape?
會認爲+號表明空格。因爲這個缺 陷,escape方法並不能正確地處理全部的非ASCII字符,你應當儘可能避免使用escape方法,取而
代之,你最好選擇 encodeURIComponent()方法。
一、將jquery.js文件中的encodeURIComponent替換爲encodeURI。便可解決中文亂碼。二、另外一種方式將全部jsp、xml的字符集都設置爲UTF-8 。
JSP <%@ page language="java" contentType="text/html; charset=UTF-8"%>XML <?xml version="1.0" encoding="UTF-8"?>。
beforeSend: function(xhr) { xhr.setRequestHeader("x-charset", "utf-8"); },膜拜啊!但是,服務端代碼怎麼寫出來的? HttpModule是什麼?HttpApplication、HttpWorkerRequest又是什麼?這個類放到哪?怎麼調用?對於仍是菜鳥的我來講頭好大啊!!
在一次次的嘗試並未解決問題後,我接了杯水,捋了捋煩躁的心情,將眼睛從新放到服務器接到的那幾個中文亂碼上,陷入迷遊:原本是GB2312編碼的、好好的中文漢字!被這個奇葩的serialize()按着UTF8編碼,又被網站默認的GB2312解碼,最終給了我一個魑魅魍魎通常的鬼漢字!艹!!
等等!這就是個人答案!在服務器上接收頁面傳參的部分對參數進行以下處理:
queryStr=context.Request["queryname"]; queryStr = HttpUtility.UrlDecode(HttpUtility.UrlEncode(queryStr, Encoding.GetEncoding("gb2312")), Encoding.UTF8);很抱歉,我用了兩行代碼。