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

还是自己一个参数一个参数的提取安全些

一个人成就的大小与承担责任的多少是成正比
原文地址:https://www.cnblogs.com/qxoffice2008/p/4012844.html