關於ASP.NET中Request.QueryString的亂碼問題(轉)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   ar   for   div   

轉自 http://www.cnblogs.com/chinhr/archive/2008/09/23/1296582.html

今天在使用Request.QueryString的時候,發現所有接收到的字串,只要是帶中文的都會變成亂碼。後來經過研究,終於找到了原因。  ASP.NET預設使用的都是UTF
-8編碼,而大家一般使用的都是GB2312編碼。這就是Request.QueryString時中文變成亂碼的原因所在,也因為這樣,才為大家帶來了很多麻煩。  下面我們來看兩個參數:“test.aspx?string=%b7%e7%a4%ce%ca%c0%bd%e7”和“test.aspx?string=%e9%a3%8e%e3%81%ae%e4%b8%96%e7%95%8c ”。粗略一看,這是給test.aspx頁面傳遞了兩個不一樣的參數,可是經過正確的URL反編碼後,可以發現這兩個其實是同一個參數:風の世界!為什麼同一個參數會得到兩個不一樣的字串呢?這是因為第一個參數是用GB2312的URL編碼的,而第二個那個則是用UTF-8編碼的。如果我們在test.aspx中直接用Request.QueryString["string"]還取值,會發現第一個參數不能正常取值,而第二個參數則正常。這是因為ASP.NET中在不指定編碼的時候,預設是使用UTF-8的編碼,自然進行URL反編碼的時候用的也是UTF-8編碼了。那麼,GB2312的URL編碼的內容,用UTF-8的URL反編碼,那肯定是會不正常的。  對於這問題,解決方案如下:  1、提交的參數是經過UTF-8 URL編碼的。  這種情況下,可以不作任何處理即可取到正常的值。例如我提交的是“test.aspx?string=%e9%a3%8e%e3%81%ae%e4%b8%96%e7%95%8c ”,那麼擷取的方法如下: ‘Visual Basic.NET Dim stringValue As String stringValue = Request.QueryString("string") Response.Write(stringValue) //Visual C# string stringValue; stringValue = Request.QueryString["string"]; Response.Write(stringValue);  2、提交的參數是經過GB2312 URL編碼的。  在這種情況下,就不能直接取值了。可以用下面的方法: ‘Visual Basic.NET ‘引用System.Collections.Specialized和System.Text命名空間 Dim stringValue As String Dim gb2312Requests As NameValueCollection gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312")) Response.Write(gb2312Requests("string")) ‘裡面的string就是你提交的參數的Key //Visual C# ‘引用System.Collections.Specialized和System.Text命名空間 string stringValue; NameValueCollection gb2312Requests; gb2312Requests = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312")) Response.Write(gb2312Requests["string"]); //‘裡面的string就是你提交的參數的Key  有的時候,我們還想提交不同編碼的URL編碼字串,可以接著往下看。  3、提交UTF8 URL編碼參數。  前面已經說過,在不指定編碼的時候,系統是自動使用UTF-8編碼的,那麼我們要提交UTF8 URL編碼參數可以直接使用Server.UrlEncode。代碼如下: ‘Visual Basic.NET Dim strBefore As String = "風の世界" Dim strAfter As String = "" strAfter = Server.UrlEncode(strBefore) Response.Write(strAlfter) //Visual C# string strBefore = "風の世界"; string strAlfter = ""; strAfter = Server.UrlEncode(strBefore); Response.Write(strAlfter);  4、提交GB2312 URL編碼參數。  因為系統預設使用的是UTF-8編碼,所以要用GB2312進行URL編碼。得指定一個編碼才行。代碼如下: ‘Visual Basic.NET ‘引用System.Text命名空間 Dim strBefore As String = "風の世界" Dim strAfter As String = "" strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312")) Response.Write(strAlfter) //Visual C# //引用System.Text命名空間 string strBefore = "風の世界"; string strAlfter = ""; strAfter = HttpUtility.UrlEncode(strBefore, Encoding.GetEncoding("GB2312")); Response.Write(strAlfter);  這樣,URL編碼後得到的就是GB2312的編碼字元了。  另外要注意的地方就是,ASP中Server.UrlEncode是以GB2312編碼進行URL編碼的。 在web.config修改如下 <globalization requestEncoding="gb2312" responseEncoding="gb2312" />

 

關於ASP.NET中Request.QueryString的亂碼問題(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.