C#如何使用HttpWebRequest、HttpWebResponse模拟浏览器抓取网页内容

时间:2021-12-29 09:49:32
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);
}