|
搜索引擎頁(yè)面 首頁(yè)為html, 頁(yè)面有個(gè)站內(nèi)搜索功能,通過(guò)提交表單方式傳參到.aspx頁(yè)面。起初不做任何操作,aspx搜索結(jié)果頁(yè)面顯示亂碼。 原因:html采用的是Gb2312 而aspx頁(yè)面默認(rèn)是utf編碼,顯然會(huì)亂碼。 正常情況下 URL編碼格式,如“漢”字: GB2312編碼:BABA URL格式:%BA%BA UTF-8 編碼:E6B189 URL格式:%E6%B1%89 1.起初我是在Page_Load里這樣操作: Encoding gb2312 = Encoding.GetEncoding("gb2312"); Response.ContentEncoding = gb2312; aspx頁(yè)面的編碼格式改為:<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 出現(xiàn)的情況是,頁(yè)面上的漢字竟然都成了口口口。
2.之后網(wǎng)上再找了些資料,通過(guò)這種方式轉(zhuǎn)化編碼。 byte[] barr = System.Text.Encoding.UTF8.GetBytes(Key); Key = System.Text.Encoding.GetEncoding("gb2312").GetString(barr); 結(jié)果有些漢字可以轉(zhuǎn)化過(guò)來(lái),但大多漢字會(huì)變成:“錕斤拷” 經(jīng)典亂碼。
原因: Unicode和老編碼體系的轉(zhuǎn)化過(guò)程中,肯定有一些字,用Unicode是沒(méi)法表示的,Unicode官方用了一個(gè)占位符來(lái)表示這些文字,這就是:U+FFFD REPLACEMENT CHARACTER 那么U+FFFD的UTF-8編碼出來(lái),恰好是 '\xef\xbf\xbd'。 如果這個(gè)'\xef\xbf\xbd',重復(fù)多次,例如 '\xef\xbf\xbd\xef\xbf\xbd',然后放到GBK/CP936/GB2312/GB18030的環(huán)境中顯示的話(huà) 一個(gè)漢字2個(gè)字節(jié),最終的結(jié)果就是:錕斤拷 哈哈。。。 此方法還是不行。 3.無(wú)奈只能修改 web.config, 因?yàn)樵撐募闹匾裕瑢?duì)其他文件也會(huì)有很大的影響,我一般很少去動(dòng),盡量在自己的頁(yè)面完成,但這次看來(lái)是不行了。 web.config文件 <globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8"/> 改為 <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="utf-8"/> 結(jié)果漢字傳參過(guò)來(lái)正確編碼,沒(méi)了亂碼。 經(jīng)過(guò)全站一番測(cè)試,問(wèn)題嚴(yán)重了,后臺(tái)很多文件都亂碼了??磥?lái)web.config文件還是不要去改,小題大作。 4.最后通過(guò)解析地址欄編碼,我接受參數(shù)的時(shí)候只要 System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(Request.Url.Query, System.Text.Encoding.GetEncoding("GB2312")); 漢字就不會(huì)亂碼了。 可是要注意的是提交表單的方法一定要是Get方式,Post方式接收不到值。 我解決問(wèn)題的步驟如下,也包括不成功的步驟 1.html頁(yè)面: <form name="Search" action="Search.aspx" method="get"> <tr> <td height="41"><img src="img/Inf_-_01.gif" width="394" height="41" /></td> <td><img src="img/Inf_-_02.gif" width="50" height="41" /></td> <td background="img/Inf_-_03.gif" width="22"></td> <td width="140" align="left" valign="bottom" background="img/Inf_-_04.gif"><label> <span class="STYLE10">站內(nèi)搜索:</span></label><input name="WordKey" type="text" id="WordKey" size="40"></td> <td width="71" valign="bottom" style="background:url(img/Inf_-_050.gif) repeat-x"><input type="image" style="width:71; height:21;" src="img/Inf__05.gif" onclick="Search();"></td> <td><img src="img/Inf_-_06.gif" width="50" height="41" /></td> <td width="127" align="center" valign="bottom" style="background:url(img/Inf_-_070.gif) repeat-x"><span class="STYLE10"><script type="text/javascript">writeDateInfo();</script></span></td> </tr> </form> 2. aspx頁(yè)面引入命名空間 using System.Text; using System.Collections.Specialized; string CurrentStr = Request.Url.Query; System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(CurrentStr, System.Text.Encoding.GetEncoding("GB2312")); string Key = nv["WordKey"]; 3.搞定。哈哈 |
|
|
來(lái)自: 昵稱(chēng)8319083 > 《html》