网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  从网上抓取指定url源码的方案     
  文章作者:未知  文章来源:水木森林  
  查看:49次  录入:管理员--2007-11-16  
 

  引言:

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

   }
  }


 
 
上一篇: java连接数据库谈    下一篇: java程序员面试32问,你能回答多少题?
  相关文档
增强的windows窗体为.net程序丰富用户界面 11-17
开发设计模式——asp.net中实现观察者模式 (1) 09-12
安全技术 java与安全性,第1部分二(图) 11-17
如何使用kxml解析wap 11-17
数据库相关:hibernate对多表关联查询 01-09
java咖啡馆(9)——一个压缩归档实用软件 11-16
java混淆编译器(转apusic.com) 11-17
深入理解java初始化的含义 11-17
简析j2ee应用程序数据库类设计模式 11-16
怎样使用ajax进行应用程序开发 11-17
文件上传下载(1) 11-17
struts测试框架strutstestcase实战 11-17
servlet容器工作原理 11-16
java & xml基础学习笔记 sax篇 11-17
java小史 11-17
给jar签名 11-17
jbuilder2005+jboss+oracle9i环境配置 11-17
jbuilder 使用中的常见问题归纳整理 11-17
开发不再是苦差事 用Eclipse简化开发 03-25
zeus实现xml-java的数据绑定 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息