从Internet上抓取指定URL的源码的方案(C#)-.NET教程,C#语言

本文介绍了一个名为getpagecode的类,该类可通过指定URL抓取网页数据,并支持使用代理服务器。抓取的数据可以保存到本地文件或以字符串形式返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言: 
  在做无线项目的时候,与通讯公司的数据通讯有一部分是通过xml交互的,所以必须要动态抓取通讯公司提供的固定的internet上的数据,便研究了一下如何抓取固定url上的数据,现与大家分享一下。 
  类名getpagecode,有一个方法getsource,通过属性传递参数,入参控制的是要取得url的地址,代理服务器的设置及输出方式的控制,这里大家可以再扩展自己的需要,我这里只提供了两种方式,一种是直接写到本地的某个文件中,另外一种就是返回字符串的。类里已经作了比较详细的注释,我想大家很容易就看明白了,如果实在不明白,那就msn上问吧,msn:yubo@x263.net。 

调用方式: 
#region 测试获取远程网页 
getpagecode gpc = new getpagecode(); 
gpc.url="http://ppcode.com"; 
gpc.proxystate=1;//使用代理服务器,0为不使用,设置为1后下面的代理设置才起作用 
gpc.proxyaddress="http://proxyname.com";//代理服务器地址 
gpc.proxyport="80";//代理服务器的端口 
gpc.proxyaccount="proxy";//代理服务器账号 
gpc.proxypassword="password";//代理服务器密码 
gpc.proxydomain="bqc";//代理服务器域 
gpc.outfilepath=filepath;//设置输出文件路径的地方,如果不设置,则返回字符串 
gpc.getsource();//处理 
string temperr=gpc.notemessage;//如果出错,这里会提示 
string tempcode=gpc.outstring;//返回的字符串 
#endregion 
类代码: 
using system; 
using system.collections; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.io; 
using system.net; 
using system.text; 
using system.web; 


namespace test.com 

/// <summary> 
/// 功能:取得internet上的url页的源码 
/// 创建:2004-03-22 
/// 作者:rexsp msn:yubo@x263.net 
/// </summary> 
public class getpagecode 

#region 私有变量 
/// <summary> 
/// 网页url地址 
/// </summary> 
private string url=null; 
/// <summary> 
/// 是否使用代码服务器:0 不使用 1 使用代理服务器 
/// </summary> 
private int proxystate=0; 
/// <summary> 
/// 代理服务器地址 
/// </summary> 
private string proxyaddress=null; 
/// <summary> 
/// 代理服务器端口 
/// </summary> 
private string proxyport=null; 
/// <summary> 
/// 代理服务器用户名 
/// </summary> 
private string proxyaccount=null; 
/// <summary> 
/// 代理服务器密码 
/// </summary> 
private string proxypassword=null; 
/// <summary> 
/// 代理服务器域 
/// </summary> 
private string proxydomain=null; 
/// <summary> 
/// 输出文件路径 
/// </summary> 
private string outfilepath=null; 
/// <summary> 
/// 输出的字符串 
/// </summary> 
private string outstring=null; 
/// <summary> 
/// 提示信息 
/// </summary> 
private string notemessage; 

#endregion 

#region 公共属性 
/// <summary> 
/// 欲读取的url地址 
/// </summary> 
public string url 

get{return url;} 
set{url=value;} 

/// <summary> 
/// 是否使用代理服务器标志 
/// </summary> 
public int proxystate 

get{return proxystate;} 
set{proxystate=value;} 

/// <summary> 
/// 代理服务器地址 
/// </summary> 
public string proxyaddress 

get{return proxyaddress;} 
set{proxyaddress=value;} 

/// <summary> 
/// 代理服务器端口 
/// </summary> 
public string proxyport 

get{return proxyport;} 
set{proxyport=value;} 

/// <summary> 
/// 代理服务器账号 
/// </summary> 
public string proxyaccount 

get{return proxyaccount;} 
set{proxyaccount=value;} 

/// <summary> 
/// 代理服务器密码 
/// </summary> 
public string proxypassword 

get{return proxypassword;} 
set{proxypassword=value;} 

/// <summary> 
/// 代理服务器域 
/// </summary> 
public string proxydomain 

get{return proxydomain;} 
set{proxydomain=value;} 

/// <summary> 
/// 输出文件路径 
/// </summary> 
public string outfilepath 

get{return outfilepath;} 
set{outfilepath=value;} 

/// <summary> 
/// 返回的字符串 
/// </summary> 
public string outstring 

get{return outstring;} 


/// <summary> 
/// 返回提示信息 
/// </summary> 
public string notemessage 

get{return notemessage;} 



#endregion 

#region 构造函数 
public getpagecode() 


#endregion 

#region 公共方法 
/// <summary> 
/// 读取指定url地址,存到指定文件中 
/// </summary> 
public void getsource() 

webrequest request = webrequest.create(this.url); 
//使用代理服务器的处理 
if(this.proxystate==1) 

//默认读取80端口的数据 
if(this.proxyport==null) 
this.proxyport="80"; 

webproxy myproxy=new webproxy(); 
myproxy = (webproxy)request.proxy; 
myproxy.address = new uri(this.proxyaddress+":"+this.proxyport); 
myproxy.credentials = new networkcredential(this.proxyaccount, this.proxypassword, this.proxydomain); 
request.proxy = myproxy; 

try 


//请求服务 
webresponse response = request.getresponse(); 
//返回信息 
stream resstream = response.getresponsestream(); 
streamreader sr = new streamreader(resstream, system.text.encoding.default); 
string tempcode= sr.readtoend(); 
resstream.close(); 
sr.close(); 

//如果输出文件路径为空,便将得到的内容赋给outstring属性 
if(this.outfilepath==null) 

this.outstring=tempcode; 

else 


fileinfo fi = new fileinfo(this.outfilepath); 
//如果存在文件则先干掉 
if(fi.exists) 
fi.delete(); 

streamwriter sw = new streamwriter(this.outfilepath,true,encoding.default); 
sw.write(tempcode); 
sw.flush(); 
sw.close(); 


catch 

this.notemessage="出错了,请检查网络是否连通;"; 




#endregion 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值