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>问题:如何能让这个变成正常的中文字呢?
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>问题:如何能让这个变成正常的中文字呢?
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();
这是我的程序中的片断,供你参考: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");
}