標籤:
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中的各種亂碼問題