c#利用HttpWebRequest获取网页源代码,搞了好几天终于解决了,直接获取网站编码进行数据读取,再也不用担心乱码了!
命名空间:Using System.Net
1 private static string GetUrlHtml(string url) 2 { 3 4 string strHtml = string.Empty; 5 6 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 7 8 HttpWebResponse response = (HttpWebResponse)request.GetResponse();//从Internet资源返回数据流 9 10 if (response.CharacterSet.ToLower() == "gbk") 11 { 12 using (Stream respStream = response.GetResponseStream())//读取数据流 13 { 14 using (StreamReader str = new StreamReader(respStream, Encoding.GetEncoding("gb2312")))//读取数据 15 { 16 strHtml = str.ReadToEnd(); 17 } 18 } 19 } 20 else 21 { 22 using (Stream respStream = response.GetResponseStream())//读取数据流 23 { 24 using (StreamReader str = new StreamReader(respStream, Encoding.UTF8))//读取数据 25 { 26 strHtml = str.ReadToEnd(); 27 } 28 } 29 } 30 return strHtml; 31 }