HttpClient

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
目前HttpClient最新版本是HttpClient 4.0.1 (GA),下载页面:[url]http://hc.apache.org/downloads.cgi[/url]
以下的测试调查例子以用户已经登录为前提:
例子:通过手机号码查询信息([url]http://www.ip138.com:8080/search.asp?[/url])
网页代码
FORM action="" method="get" name="mobileform" οnsubmit="return checkMobile();">
<TR bgColor=#eff1f3class=tdc>
<TD align=middle width=130 noswap>手机号码(段) </TD>
[color=blue]<TD align=middle width=*><INPUT class=tdc name="mobile" maxLength="11">[/color][color=red]<!--找到输入框名,从url也能看出来-->[/color]
<INPUT name="action" type="hidden" value=mobile> <INPUT class=bdtj name=B1 type=submit value="查 询"></TD>
</TR>
</FORM>
<TR class=tdc bgcolor=#EFF1F3>
<TD width="130" align="center" noswap>您查询的手机号码段</TD>
<TD width=* align="center" class=tdc2>13601695</TD>
</TR>
<TR class=tdc bgcolor=#EFF1F3>
<TD width="130" align="center" noswap>卡号归属地</TD>
<TD width=* align="center" class=tdc2>上海 上海</TD>
</TR>
<TR class=tdc bgcolor=#EFF1F3>
<TD width="130" align="center" noswap>卡 类 型</TD>[color=red]<!--显示数据,共同点为class都是tdc2,可以在分析html的过程中作为过滤标准-->[/color]
[color=blue]<TD width=* align="center" class=tdc2>移动全球通卡</TD>[/color]
</TR>
<TR class=tdc bgcolor=#EFF1F3>
<TD align="center">区 号</TD>
[color=blue]<TD align="center" class=tdc2>021</TD>[/color]
</TR>
<TR class=tdc bgcolor=#EFF1F3>
<TD align="center">邮 编</TD>
<[color=blue]TD align="center" class=tdc2>200000</TD>[/color]
以上分析,得到了使用HttpClient的必要条件: url, 参数, 以及htmlparser的过滤条件
利用HttpClient查询,为了分析提取获取的html内容,我们使用htmlparser包,其主页:http://htmlparser.sourceforge.net/
在Eclipse中建立Telephone类,除了main()以外,包括2个方法:
调用HttpClient,从指定url获得responseString并返回。
调用HtmlParser分析html文件:
public static String getPostString(String mobile) throws HttpException, IOException
{
HttpClient hc=new HttpClient();
PostMethod pm=new PostMethod("http://www.ip138.com:8080/search.asp"); /*检索页面的url*/
hc.getParams().setContentCharset("gb2312");
pm.addParameter("mobile",mobile); /*检索页面的参数1*/
pm.addParameter("action","mobile"); /*检索页面的参数2*/
hc.executeMethod(pm);
System.out.println("GET POST STRING: "+pm.getResponseBodyAsString());
return pm.getResponseBodyAsString();
}
public static void getMobileInfor(String poststring) throws ParserException
{
Parser parser=new Parser(poststring);
NodeList nodelist=null;
NodeFilter filter=new HasAttributeFilter("class","tdc2"); /*过滤页面的内容,提炼出class为tdc2的内容*/
nodelist=parser.extractAllNodesThatMatch(filter);
for(int i=0;i<nodelist.size();i++)
{
System.out.println("data"+i+ ":"+nodelist.elementAt(i).toPlainTextString().replace(" ",""));
}
}

查看运行结果如下附件
推荐一个学习HttpClient的好地方[url]http://htmlparser.com.cn/post/20090816119.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值