您现在的位置是:网站首页> 编程资料编程资料
ASP.NET抓取网页内容的实现方法_实用技巧_
2023-05-24
427人已围观
简介 ASP.NET抓取网页内容的实现方法_实用技巧_
本文实例讲述了ASP.NET抓取网页内容的实现方法。分享给大家供大家参考。具体实现方法如下:
一、ASP.NET 使用HttpWebRequest抓取网页内容
复制代码 代码如下:
/// 方法一:比较推荐
/// 用HttpWebRequest取得网页源码
/// 对于带BOM的网页很有效,不管是什么编码都能正确识别
///
/// 网页地址"
///返回网页源文件
public static string GetHtmlSource2(string url)
{
//处理内容
string html = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*"; //接受任意文件
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //
request.AllowAutoRedirect = true;//是否允许302
//request.CookieContainer = new CookieContainer();//cookie容器,
request.Referer = url; //当前页面的引用
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
html = reader.ReadToEnd();
stream.Close();
return html;
}
/// 用HttpWebRequest取得网页源码
/// 对于带BOM的网页很有效,不管是什么编码都能正确识别
///
/// 网页地址"
///
public static string GetHtmlSource2(string url)
{
//处理内容
string html = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*"; //接受任意文件
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //
request.AllowAutoRedirect = true;//是否允许302
//request.CookieContainer = new CookieContainer();//cookie容器,
request.Referer = url; //当前页面的引用
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.Default);
html = reader.ReadToEnd();
stream.Close();
return html;
}
二、ASP.NET 使用 WebResponse 抓取网页内容
复制代码 代码如下:
public static string GetHttpData2(string Url)
{
string sException = null;
string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
sException = e.Message.ToString();
}
catch (Exception e)
{
sException = e.ToString();
}
finally
{
if (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
{
string sException = null;
string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
sException = e.Message.ToString();
}
catch (Exception e)
{
sException = e.ToString();
}
finally
{
if (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
相关内容
- .net中webconfig 详解_实用技巧_
- 浅谈ASP.NET中最简单的自定义控件_实用技巧_
- Asp.net获取服务器指定文件夹目录文件并提供下载的方法_实用技巧_
- ASP.Net下载大文件的实现方法_实用技巧_
- ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)_实用技巧_
- 使用ASP.NET.4.5.1+MVC5.0 搭建一个包含 Ninject框架 项目_实用技巧_
- ASP.NET WebForm中<%=%>与<%#%>的区别_实用技巧_
- ASP.NET从客户端中检测到有潜在危险的request.form值的3种解决方法_实用技巧_
- .net 解决spider多次和重复抓取的方案_实用技巧_
- asp.net+ajax的Post请求实例_实用技巧_
