日期:2014-05-19  浏览次数:20912 次

下载网页乱码,忙解决本网站
private   string   DownHtml(string   url)
                {
                      WebClient   webc   =   new   WebClient();
                        webc.Credentials   =   CredentialCache.DefaultCredentials;
                        Byte[]   pageData   =   webc.DownloadData(url);
                      return   Encoding.Default.GetString(pageData);
                }

本代码得到大部份网站编正常可碰到
http://news.csdn.net/n/20070510/103850.html
这个网站就为乱码了,希望高手帮忙解决,谢谢!

------解决方案--------------------
一定要用WebClient吗,WebRequestr用下面的测试通过

private string DownHtml(string url)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24 ";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.Default);
return (sr.ReadToEnd());
resStream.Close();
sr.Close();
}