解決ASP.NET中的各種亂碼問題

來源:互聯網
上載者:User

標籤:

http://www.cnblogs.com/fish-li/archive/2012/10/14/2723631.html

C# URL 中文編碼與解碼

參考資料

http://www.sosuo8.com/article/show.asp?id=3036

http://blog.csdn.net/zhongzhengfeng/article/details/3236551

http://www.mxcz.net/tools/Url.aspx

非常蛋疼的事情, google 和 baidu 在編碼是分別採用了 UTF-8 和 GB2312

基礎知識

UTF-8中,一個漢字對應三個位元組,GB2312中一個漢字佔用兩個位元組。 
不論何種編碼,字母數字都不編碼,特殊符號編碼後佔用一個位元組。

自動解碼

public static string MyUrlDeCode(string str, Encoding encoding){    if (encoding == null)    {        Encoding utf8 = Encoding.UTF8;        //首先用utf-8進行解碼                            string code = HttpUtility.UrlDecode(str.ToUpper(), utf8);        //將已經解碼的字元再次進行編碼.        string encode = HttpUtility.UrlEncode(code, utf8).ToUpper();        if (str == encode)            encoding = Encoding.UTF8;        else            encoding = Encoding.GetEncoding("gb2312");    }    return HttpUtility.UrlDecode(str, encoding);}

More

謹慎使用 Request["foo"] 擷取參數,反正就是各種bug

還是自己一個參數一個參數的提取安全些

解決ASP.NET中的各種亂碼問題

聯繫我們

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