C#获取⽹页源代码的⽅法
本⽂实例讲述了C#获取⽹页源代码的⽅法。分享给⼤家供⼤家参考。具体如下:
public string GetPageHTML(string url)
{
try
{
HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
wr.Method = "get";
wr.Accept = "*/*";
wr.Headers.Add("Accept-Language: zh-cn");
wr.Headers.Add("UA-CPU: x86");
wr.Headers.Add("Accept-Encoding: gzip, deflate");
wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Embedded Web Browser from: bsalsa/; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; wr.KeepAlive = true;
wr.ServicePoint.Expect100Continue = false;
wr.AllowAutoRedirect = false;
HttpWebResponse wre = wr.GetResponse() as HttpWebResponse;
StreamReader sreader = new StreamReader(wre.GetResponseStream(), Encoding.GetEncoding("GBK"));
string sHtml = sreader.ReadToEnd();
wre.Close();
return sHtml;
iframe嵌套页面加载慢}
catch
{
return "";
}
}
希望本⽂所述对⼤家的C#程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论