我用WebRequest类向一个http服务器获取信息,服务器返回给我的是一些类似:%C4%E3%BA%C3%B0%A1%A3%A1%2B_-)(*%26%5E这样的信息,我知道这个字符是经过unicode编码的,例如最后的一个%5E其实就代表“^”字符。用UnicodeEncoding.GetString()方法好像不行。

解决方案 »

  1.   

    System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3%B0%A1%A3%A1",System.Text.Encoding.Default);
      

  2.   

    byte[] bytes = System.Text.Encoding.Default.GetBytes(code); 

    string decode =  System.Text.Encoding.UTF8.GetString(bytes);
      

  3.   

    给你一个方法,看这个方法,你可以了解到编码过程
    public static string FromUtf8(string str)
            {
                if (str == null)
                {
                    return string.Empty;
                }
                else
                {                StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < str.Length; ++i)
                    {
                        if (i < (str.Length - 2))
                        {
                            if (str.Substring(i, 3).Equals("%3A"))
                            {
                                sb.Append(':');                            i++;
                                i++;
                            }
                            else if (str.Substring(i, 3).Equals("%2E"))
                            {
                                sb.Append('.');
                                i++;
                                i++;
                            }
                            else if (str.Substring(i, 3).Equals("%2F"))
                            {
                                sb.Append('/');
                                i++;
                                i++;
                            }
                            else
                            {
                                sb.Append(str[i]);
                            }
                        }
                        else
                        {
                            sb.Append(str[i]);
                        }
                    }
                    return sb.ToString();
                }
            }
      

  4.   

    Sydney_Carton(悉尼盒子)
     hanmlxiao(hanml)的方法都不行,至于zhchg6666,这段程序难道就只能够解码几个标点符号吗?
    另外,那个HttpUtility是不能用在WinForm程序中的。
    请大家回复的时候自己先测试一下!其实我想比较苯的方法就是将每一个%XX转换为十进制数字,然后再用GetString方法还原字符串。不过我想.NET平台应该有现成的方法吧?自己写有点不太合适。
      

  5.   

    个人感觉%C4%E3%BA%C3%B0%A1%A3%A1之类是urlencode后的结果,urldecode结果是"你好啊!"
    另外httputility是可以用在winform下的,不过你得引用System.Web.dll
      

  6.   

    请问楼上的如何引用System.Web.dll?
      

  7.   

    接受aluzi的批评 重新写了一个解析utf8的函数 private void button2_Click(object sender, EventArgs e)
            {
                string a = "你好啊!";
                a = ToUtf8(a);
                a = FromUtf8(a);
                textBox1.Text = a;
            }        //用于将字符串转换成UTF-8编码
            private static string ToUtf8(string str)
            {
                if (str == null)
                {
                    return string.Empty;
                }
                else
                {
                    char[] hexDigits = {  '0', '1', '2', '3', '4', 
                                      '5', '6', '7', '8', '9', 
                                      'A', 'B', 'C', 'D', 'E', 'F'};                Encoding utf8 = Encoding.UTF8;
                    StringBuilder result = new StringBuilder();                for (int i = 0; i < str.Length; i++)
                    {
                        string sub = str.Substring(i, 1);
                        byte[] bytes = utf8.GetBytes(sub);                    for (int j = 0; j < bytes.Length; j++)
                        {
                            result.Append("%" + hexDigits[bytes[j] >> 4] + hexDigits[bytes[j] & 0XF]);
                        }
                    }                return result.ToString();
                }
            }        //将UTF-8编码转换成字符串
            public string FromUtf8(string str)
            {
                char[] hexDigits = {  '0', '1', '2', '3', '4', 
                                      '5', '6', '7', '8', '9', 
                                      'A', 'B', 'C', 'D', 'E', 'F'};
                List<byte> byteList = new List<byte>(str.Length / 3);            if (str != null)
                {
                    List<string> strList = new List<string>();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < str.Length; ++i)
                    {
                        if (str[i] == '%')
                        {
                             strList.Add(str.Substring(i, 3));
                        }                    
                    }                foreach (string tempStr in strList)
                    {
                        int num = 0;
                        int temp = 0;
                        for (int j = 0; j < hexDigits.Length; ++j)
                        {
                            if (hexDigits[j].Equals(tempStr[1]))
                            {
                                temp = j ;
                                num = temp << 4; 
                            }
                        }                    for (int j = 0; j < hexDigits.Length; ++j)
                        {
                            if (hexDigits[j].Equals(tempStr[2]))
                            {
                                num += j;
                            }
                        }                    byteList.Add((byte)num);
                    }
                }            return Encoding.UTF8.GetString(byteList.ToArray());
            }经过测试,可以来回转换字符串和utf8
      

  8.   

    zhchg6666() 你好强!
    衷心感谢你的工作!