Uri uri = new Uri("http://www.nmc.gov.cn/radar/radar.php?No=anec");
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
html = sr.ReadToEnd();//此处是源码 ,html为变量
提取的网页为http://www.nmc.gov.cn/radar/radar.php?No=anec有时候好用,有的时候为乱码,愁死我了。望大虾们多指点!!
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
html = sr.ReadToEnd();//此处是源码 ,html为变量
提取的网页为http://www.nmc.gov.cn/radar/radar.php?No=anec有时候好用,有的时候为乱码,愁死我了。望大虾们多指点!!
调整下这里试试
public Encoding GetEncoding(HttpWebResponse response)
{
string name = response.ContentEncoding;
Encoding code = Encoding.Default;
if (name == "")
{
string contentType = response.ContentType;
if (contentType.ToLower().IndexOf("charset") != -1)
{
name = contentType.Substring(contentType.ToLower().IndexOf("charset=") "charset=".Length);
}
}
if (name != "")
{
try
{
code = Encoding.GetEncoding(name);
}
catch{}
}
return code;
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
是gb2312啊,关键有的时候好用啊,有时候才是乱码,当乱码的时候我怎么改System.Text.Encoding.Default 都不好使!诡异死了!
不妨试下指定编码的方式,比如使用System.Text.Encoding.GetEncoding("gb2312")替换System.Text.Encoding.Default
“改System.Text.Encoding.Default”?这就谈不上“改”了。在中文操作系统上,它总是gb2312,这就是Default的含义。不要乱用Default。