public string GetHtml(string url, Encoding ed)
{
string Html = string.Empty;//初始化新的webRequst
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
Request.KeepAlive = true;
Request.ProtocolVersion = HttpVersion.Version11;
Request.Method = "GET";
Request.Accept = "*/* ";
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
Request.Referer = url;
HttpWebResponse htmlResponse = (HttpWebResponse)Request.GetResponse();
//从Internet资源返回数据流
Stream htmlStream = htmlResponse.GetResponseStream();
//读取数据流
StreamReader weatherStreamReader = new StreamReader(htmlStream, ed);
//读取数据
Html = weatherStreamReader.ReadToEnd();
weatherStreamReader.Close();
htmlStream.Close();
htmlResponse.Close();
//针对不同的网站查看html源文件
return Html;
}
public string GetHtml(string url)
{
return GetHtml(url, Encoding.UTF8);
}
本文标签:使用 HttpWebRequest HttpWebResponse 模拟 浏览器 抓取 网页 内容
本文链接: https://www.51dev.com/dnet/126463
版权说明: 原创文章,未经授权请勿转载。