WebRequest wbr =WebRequest.Create(uri);
Stream rc=wbr.GetResponse().GetResponseStream();
wbr.Method = "Get"; 
StreamReader read=new StreamReader(rc,System.Text.Encoding.GetEncoding("GB2312")); 
string s=read.ReadToEnd();结果出现乱码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body>
<a href="http://www.sina.com.cn/bbs">婀栧浗</a> 
</body>
</html>问题:如何能让这个变成正常的中文字呢?

解决方案 »

  1.   

    what is the right encoding?StreamReader read=new StreamReader(rc,System.Text.Encoding.GetEncoding("GB2312")); 
    string s=read.ReadToEnd();
    Console.WriteLine(read.CurrentEncoding.EncodingName );you might want to tryStreamReader read=new StreamReader(rc,System.Text.Encoding.UTF8); 
    string s=read.ReadToEnd();
      

  2.   

    把你的<meta http-equiv="Content-Type" content="text/html; charset=utf-8">改掉。
      

  3.   

    其实使用任何一种Encoding英文字符和网页的meta data都不过出乱码,所以我的方法是先使用一种随便什么Encoding把网页down下来,然后使用正则表达式分析出charset,第二次改用这个charset指定的Encoding down网页的内容。但是事实证明有的网页中不包含charset这个信息(我试过了),我觉得一般使用gb2312出乱码的可能比较小。
    这是我的程序中的片断,供你参考:private Regex regEncoding = new Regex(@"charset=([\w-]*)\.*"); /// <summary>
    /// Get a charset information from the target web uri;
    /// </summary>
    /// <param name="targetUri">Target web Uri</param>
    /// <returns>The Encoding the website used</returns>
    public Encoding GetWebEncoding(Uri targetUri)
    {
    Encoding enc = Encoding.ASCII;
    WebClient browser = new WebClient();
    string strContent = enc.GetString(browser.DownloadData(targetUri.ToString())); Match m = regEncoding.Match(strContent);
    if(m.Success)
    {
    return Encoding.GetEncoding(m.Groups[1].ToString());
    }
    return Encoding.GetEncoding("gb2312");
    }