阅 读 文 章

用Java从网上抓取指定URL源码的方案

[来源:网上转载 (http://www.chinaunix.net) | 作者:网友(不详) | 时间:2007-07-07 | 浏览:人次 ]

引言:
  在做无线项目的时候,与通讯公司的数据通讯有一部分是通过XML交互的,所以必须要动态抓取通讯公司提供的固定的Internet上的数据,便研究了一下如何抓取固定url上的数据,现与大家分享一下。

  类名GetPageCode,有一个方法GetSource,通过属性传递参数,入参控制的是要取得URL的地址,代理服务器的设置及输出方式的控制,这里大家可以再扩展自己的需要,我这里只提供了两种方式,一种是直接写到本地的某个文件中,另外一种就是返回字符串的。类里已经作了比较详细的注释,我想大家很容易就看明白了。

调用方式:
#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
{
/// &lt summary &gt
/// 功能:取得Internet上的URL页的源码
/// 创建:2004-03-22
/// 作者:Rexsp MSN:yubo@x263.net
/// &lt /summary &gt
public class GetPageCode
{
#region 私有变量
/// &lt summary &gt
/// 网页URL地址
/// &lt /summary &gt
private string url=null;
/// &lt summary &gt
/// 是否使用代码服务器:0 不使用 1 使用代理服务器
/// &lt /summary &gt
private int proxyState=0;
/// &lt summary &gt
/// 代理服务器地址
/// &lt /summary &gt
private string proxyAddress=null;
/// &lt summary &gt
/// 代理服务器端口
/// &lt /summary &gt
private string proxyPort=null;
/// &lt summary &gt
/// 代理服务器用户名
/// &lt /summary &gt
private string proxyAccount=null;
/// &lt summary &gt
/// 代理服务器密码
/// &lt /summary &gt
private string proxyPassword=null;
/// &lt summary &gt
/// 代理服务器域
/// &lt /summary &gt
private string proxyDomain=null;
/// &lt summary &gt
/// 输出文件路径
/// &lt /summary &gt
private string outFilePath=null;
/// &lt summary &gt
/// 输出的字符串
/// &lt /summary &gt
private string outString=null;
/// &lt summary &gt
/// 提示信息
/// &lt /summary &gt
private string noteMessage;

#endregion

#region 公共属性
/// &lt summary &gt
/// 欲读取的URL地址
/// &lt /summary &gt
public string Url
论坛热门帖子: [lch203] 写得蛮好的linux学习笔记(10-21)
[黑马制造] 学习java的30个目标(10-19)
[笑傲股林] 做测试半年了,有点迷茫,应该再学些什么提高自己的测试水平和测试能力呢?(10-19)
[udp8589] 大家用google的来吱一声? 用百度的~~也来报道下?(10-18)
[沂偌掳兆] 本人总结的一些认为C++比较经典的书籍,希望对大家有用(10-18)
TAG标签: 源码 方案 指定 网上 /// lt gt /summary summary string

最新评论 共有0位网友发表了评论

发表评论

评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名:(注册)
密码:
验证码:
匿名发表

网站地图友情连接交流论坛网站投稿广告服务联系我们留言本站长统计
Some rights reserved: www.chmhome.com, 鄂ICP备07010232号 E-mail:chinakafei@live.com,QQ:552766
中国咖啡技术网(Chmhome):国外编程技术书籍,中文编程手册,经典编程文章,交流技术,技术软件下载,计算机论文,毕业论文.