c# 远程获取网页内容,防止乱码,页面获取不全

View Code
 1         public static string GetWebContent(string url)
 2         {
 3             string result = string.Empty;
 4             try
 5             {
 6                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
 7                 myHttpWebRequest.AllowAutoRedirect = true;
 8 
 9                 //maximum of 10 auto redirects 
10                 myHttpWebRequest.MaximumAutomaticRedirections = 10;
11                 //30 second timeout for request 
12                 myHttpWebRequest.Timeout = (int)new TimeSpan(0060).TotalMilliseconds; 
13                 myHttpWebRequest.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1)";
14                 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
15                 Stream webStream = myHttpWebResponse.GetResponseStream();
16                 string encoding = myHttpWebResponse.CharacterSet;
17                 //经过大量测试发现.NET会将gb2312的编码格式识别为ISO-8859-1,故作此处理
18                 encoding = (encoding == "ISO-8859-1") ? "gb2312" : encoding;
19                 //获取的内容是否属于文本
20                 char seperator = '/';
21                 String contenttype = myHttpWebResponse.ContentType;
22                 // 返回 'text' 如果文本类型是'text/html.
23                 String maintype = contenttype.Substring(0, contenttype.IndexOf(seperator));
24                 // 只保存内容为'text'类型的网页,不保存示图片等其他文件
25                 if (String.Compare(maintype, "text") == 0)
26                 {
27                     StreamReader sw = new StreamReader(webStream, System.Text.Encoding.GetEncoding(encoding));
28                     result = sw.ReadToEnd();
29                     sw.Close();
30                     sw.Dispose();
31                     webStream.Close();
32                 }
33             }
34             catch (Exception ex)
35             {
36                 LogHelper.Error(ex);
37             }
38             return result;
39         }
40     }
原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/2249897.html