引言:
在做无线项目的时候,与通讯公司的数据通讯有一部分是通过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
}
}
在做无线项目的时候,与通讯公司的数据通讯有一部分是通过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
}
}