-
C#教程之C#网页信息采集方法汇总
本文实例总结了三种常用的C#网页信息采集方法。分享给大家供大家参考。具体实现方法如下:
一、通过HttpWebResponse 来获取
复制代码 代码如下:
public static string CheckTeamSiteUrl(string url)
{
string response = "";
HttpWebResponse httpResponse = null;
//assert: user have access to URL
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Headers.Set("Pragma", "no-cache");
// request.Headers.Set("KeepAlive", "true");
httpRequest.CookieContainer = new CookieContainer();
httpRequest.Referer = url;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 403 Access denied, URL: " + url, ex);
}
//if here, the URL is correct and the user has access
try
{
string strEncod = httpResponse.ContentType;
StreamReader stream;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
}
else
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default);
}
char[] buff = new char[4000];
stream.ReadBlock(buff,0,4000);
response = new string(buff);
stream.Close();
httpResponse.Close();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 404 Page not found, URL: " + url, ex);
}
return response;
}
{
string response = "";
HttpWebResponse httpResponse = null;
//assert: user have access to URL
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Headers.Set("Pragma", "no-cache");
// request.Headers.Set("KeepAlive", "true");
httpRequest.CookieContainer = new CookieContainer();
httpRequest.Referer = url;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 403 Access denied, URL: " + url, ex);
}
//if here, the URL is correct and the user has access
try
{
string strEncod = httpResponse.ContentType;
StreamReader stream;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
}
else
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default);
}
char[] buff = new char[4000];
stream.ReadBlock(buff,0,4000);
response = new string(buff);
stream.Close();
httpResponse.Close();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 404 Page not found, URL: " + url, ex);
}
return response;
}
二、通过 WebResponse 来获取
复制代码 代码如下:
public static string getPage(String url)
{
WebResponse result = null;
string resultstring = "";
try
{
WebRequest req = WebRequest.Create(url);
req.Timeout = 30000;
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
//read the stream into a string
//StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
string strEncod = result.ContentType;
StreamReader sr;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
}
else
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.Default);
}
resultstring = sr.ReadToEnd();
js.alert(resultstring);
//Console.WriteLine(resultstring);
}
catch
{
throw new Exception();
}
finally
{
if (result != null)
{
result.Close();
}
}
return resultstring;
}
{
WebResponse result = null;
string resultstring = "";
try
{
WebRequest req = WebRequest.Create(url);
req.Timeout = 30000;
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
//read the stream into a string
//StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
string strEncod = result.ContentType;
StreamReader sr;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
}
else
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.Default);
}
resultstring = sr.ReadToEnd();
js.alert(resultstring);
//Console.WriteLine(resultstring);
}
catch
{
throw new Exception();
}
finally
{
if (result != null)
{
result.Close();
}
}
return resultstring;
}
三、通过WebClient来获取
复制代码 代码如下:
public string get(int length)
{
try
{
getEncodeing();
WebClient wb = new WebClient();
Stream response = wb.OpenRead(url);
StreamReader reader = new StreamReader(response, this.encoding, true, 256000);
char[] a = new char[length];
int i = reader.Read(a,0,length);
reader.Close();
return new string(a);
}
catch (Exception e)
{
return e.Message;
//return null;
}
}
private void getEncodeing()
{
switch (this.encode)
{
case "UTF-8": encoding = Encoding.UTF8; break;
case "GB2312": encoding = Encoding.GetEncoding("GB2312"); break;
case "ASCII": encoding = Encoding.ASCII; break;
default: encoding = Encoding.GetEncoding(encode); break;
}
}
{
try
{
getEncodeing();
WebClient wb = new WebClient();
Stream response = wb.OpenRead(url);
StreamReader reader = new StreamReader(response, this.encoding, true, 256000);
char[] a = new char[length];
int i = reader.Read(a,0,length);
reader.Close();
return new string(a);
}
catch (Exception e)
{
return e.Message;
//return null;
}
}
private void getEncodeing()
{
switch (this.encode)
{
case "UTF-8": encoding = Encoding.UTF8; break;
case "GB2312": encoding = Encoding.GetEncoding("GB2312"); break;
case "ASCII": encoding = Encoding.ASCII; break;
default: encoding = Encoding.GetEncoding(encode); break;
}
}
希望本文所述对大家的C#程序设计有所帮助。
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比