ASP.NET中與編碼方式相關的問題

來源:互聯網
上載者:User
一.在Asp.net的HttpCookie中寫入漢字,讀取時為亂碼
其實這是因為文字編碼而造成的,漢字是兩個編碼,所以才會搞出這麼個亂碼出來!其實解決的方法很簡單:只要在寫入Cookie時,先將其用Url編碼,然後再寫入,當我們讀取時再解碼就OK了.

  例子:
  Cookie的寫入:
   HttpCookie cookie=new HttpCookie("Simple");
   cookie.Values.Add("Simple1",HttpUtility.UrlEncode("中文字元測試!"));
   cookie.Values.Add("Simple2","The English Charactor Test!");
   Response.AppendCookie(cookie);
  Cookie的讀取:
   HttpCookie cookie=Request.Cookies["Simple"];
   string simple1=HttpUtility.UrlDecode(cookie["Simple1"]);
   string simple2=cookie["Simple2"];
  
  這樣
  simple1="中文字元測試!";
  simple2="The English Charactor Test!";

二.地址欄中傳遞帶中文字元的參數

傳遞參數時先將其進行urlencode編碼,然後再賦給URL串連,避免擷取中文參數時,取出亂碼的問題;
例子:
string Text = "地址欄中傳遞帶中文字元的參數"
Text = System.Web.HttpUtility.UrlEncode(Text, System.Text.Encoding.GetEncoding("GB2312"));

三.匯出EXCEL時與編碼相關的問題
...... 
 
四.把UTF-8編碼轉換為GB2312編碼

最近在做的廣告系統中,碰到了一個問題,廣告系統採用的UTF-8編碼,而一些使用這套廣告系統的頻道頁面使用的是GB2312編碼。當然也有使用UTF-8編碼的頻道使用這套廣告系統。

頻道頁面是通過嵌入類似如下的代碼方式,來調用廣告的。具體那個時間顯示那個廣告,或者那些廣告組合是廣告系統自己處理的。

<script type="text/javascript">
<!--
csdn_AD_Position_GroupID = "{f05ff3bf-246b-4d71-a101-b5d4ee3f6cd3}";
csdn_AD_Page_Url = document.location;
//-->
</script>
<script type="text/javascript" src=http://ads.csdn.net/AD/Show_js_AD.js >
</script>

不同編碼的頁面、指令碼之間互相引用,就會產生亂碼的問題,解決方案就是統一成一種編碼。
asp.net 中,如果要修改輸出頁面的編碼,可以通過修改web.config中以下配置資訊

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

以上只是修改整體的預設編碼,如果只有某個頁的編碼需要修改,ASP.net 中則可以簡單的使用下面代碼:

Encoding gb2312 = Encoding.GetEncoding("gb2312");Response.ContentEncoding = gb2312;

在非ASP.net 應用中,可能你讀到的資料是UTF-8編碼,但是你要轉換為GB2312編碼,則可以參考以下代碼:

 

string utfinfo = "document.write(/"alert('aa你好嗎??');/");";string gb2312info = string.Empty;Encoding utf8 = Encoding.UTF8;Encoding gb2312 = Encoding.GetEncoding("gb2312");// Convert the string into a byte[].byte[] unicodeBytes = utf8.GetBytes(utfinfo);// Perform the conversion from one encoding to the other.byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);// Convert the new byte[] into a char[] and then into a string.// This is a slightly different approach to converting to illustrate// the use of GetCharCount/GetChars.char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);gb2312info = new string(asciiChars);

當然,其他各種編碼之間的轉換,跟上述代碼也類似的,就不描述了。

五.ASP.NET自動識別GB2312與UTF-8編碼的檔案

http://www.webjx.com  更新日期:2007-10-15 22:27  出處:網頁教學網

Question

在簡體中文系統中,我們有時候需要開啟一個儲存在磁碟上的純文字檔案,例如txt,但卻不知道其儲存編碼,該怎麼辦呢?

如果文本只需要在Windows上顯示,那就很幸運了,因為無論是GB2312還是UTF-8編碼的string都能夠正確顯示。但如果需要輸出到ASP.NET頁面上就不是那麼簡單了,因為如果頁面編碼為UTF-8但純文字檔案匯入的string是GB2312,那就會導致亂碼,反之亦然。因此,我們需要一種方法自動識別磁碟上的純文字檔案到底是麼編碼的。

Answer

StreamReader其實是有編碼自動檢測功能,不過因為它僅僅檢測前3個位元組,所以只能UTF-8、Little-Endian Unicode、Big-Endian Unicode之前作出選擇,如果上述3個都不匹配,就選擇使用者提供的編碼。因此,我們可以提供GB2312編碼給StreamReader,讓它在GB2312和UTF-8之前自動作出選擇。

using (StreamReader reader = new StreamReader(path, Encoding.Default)
{
  string line;
  while ((line = reader.ReadLine()) != null)
  {
    FileTextBox.InnerHtml += Server.HtmlEncode(line) + " ";
  }
  FileEncodingNameLabel.Text = reader.CurrentEncoding.EncodingName;
}

上述代碼使用Encoding.Default初始化StreamReader,Encoding.Default指的是系統的預設ANSI編碼,在簡體中文系統中就是GB2312。這樣寫能夠讓代碼保持有一定的相容性,例如在繁體中文系統就變成了自動在Big5和UTF-8之間做出選擇。之後的代碼就是用StreamReader逐行讀取檔案內容並放到FileTextBox中,最後通過StreamReader的CurrentEncoding屬性擷取編碼名稱並顯示在FileEncodingNameLabel中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.