tambc
BlogJava
首頁
新隨筆
聯系
聚合
管理
5 Posts :: 21 Stories :: 5 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2007年2月 (2)
2006年12月 (3)
文章分類
Ajax
C#(1)
Delphi(3)
Hibernate(1)
Java(14)
JavaScript(7)
Oracle
Spring
SQLServer(1)
Struts
全文檢索(3)
工作流學習
文章檔案
2007年2月 (1)
2007年1月 (1)
2006年12月 (16)
搜索
最新評論
1.?re: textarea控制字符數
收益匪淺
--ljh
2.?re: textarea控制字符數
評論內容較長,點擊標題查看
--fdsa
3.?re: 免費部署 與同時訪問此網頁的網友聊天[未登錄]
股票
--秋風
4.?re: 實現HTTP長連接(服務器推)
yao lianjie
--張偉
5.?re: 近來使用velocity來生成網站靜態頁面
re: 近來使用velocity來生成網站靜態頁面的地址
--gv
閱讀排行榜
1.?近來使用velocity來生成網站靜態頁面(1393)
2.?轉:基于Java的開源 Carrot2 搜索結果聚合聚類引擎 2.0(700)
3.?使用C3P0時一個怪異的事情(471)
4.?用JS讓查看源代碼時什么也看不到(410)
5.?免費部署 與同時訪問此網頁的網友聊天(337)
評論排行榜
1.?免費部署 與同時訪問此網頁的網友聊天(1)
2.?近來使用velocity來生成網站靜態頁面(1)
3.?用JS讓查看源代碼時什么也看不到(0)
4.?轉:基于Java的開源 Carrot2 搜索結果聚合聚類引擎 2.0(0)
5.?使用C3P0時一個怪異的事情(0)
HttpClient和HtmlParser配合實現自動登陸系統抽取頁面信息
/**/
/*
?*?Main.java
?*
?*?Created?on?2007年1月19日,?上午9:14
?*
?*?To?change?this?template,?choose?Tools?|?Template?Manager
?*?and?open?the?template?in?the?editor.
?
*/
package
?wapproxy;
import
?org.apache.commons.httpclient.
*
;
import
?org.apache.commons.httpclient.methods.
*
;
import
?org.apache.commons.httpclient.params.HttpMethodParams;
import
?java.io.
*
;
import
?org.htmlparser.Node;
import
?org.htmlparser.NodeFilter;
import
?org.htmlparser.Parser;
import
?org.htmlparser.filters.TagNameFilter;
import
?org.htmlparser.tags.
*
;
import
?org.htmlparser.util.NodeList;
import
?org.htmlparser.util.ParserException;
?
/**?*/
/**
?*
?*?
@author
?xcz
?
*/
public
?
class
?Main?
{
????
????
????
/**?*/
/**
?Creates?a?new?instance?of?Main?
*/
????
public
?Main()?
{
????}
????
????
/**?*/
/**
?????*?
@param
?args?the?command?line?arguments
?????
*/
????
public
?
static
?
void
?main(String[]?args)?
throws
?Exception??
{
????????
//
?Create?an?instance?of?HttpClient.
????????HttpClient?client?
=
?
new
?HttpClient();
????????
????????
//
?Create?a?method?instance.
????????PostMethod?post_method?
=
?
new
?PostMethod(
"
http://localhost/rcpq/
"
);
????????
????????NameValuePair[]?data?
=
?
{
????????????
new
?NameValuePair(
"
username
"
,?
"
admin
"
),
????????????
new
?NameValuePair(
"
password
"
,?
"
admin
"
),
????????????
new
?NameValuePair(
"
dologin
"
,?
"
1
"
),
????????}
;
????????
????????post_method.setRequestBody(data);
????????
????????
????????
????????
try
?
{
????????????
//
?Execute?the?method.
????????????
int
?statusCode?
=
?client.executeMethod(post_method);
????????????
????????????
if
?(statusCode?
!=
?HttpStatus.SC_OK)?
{
????????????????System.err.println(
"
Method?failed:?
"
?
+
?post_method.getStatusLine());
????????????}
????????????
????????????
//
?Read?the?response?body.
????????????
//
byte[]?responseBody?=?post_method.getResponseBody();
????????????
????????????
//
?Deal?with?the?response.
????????????
//
?Use?caution:?ensure?correct?character?encoding?and?is?not?binary?data
????????????
//
System.out.println(new?String(responseBody));
????????????
????????}
?
catch
?(HttpException?e)?
{
????????????System.err.println(
"
Fatal?protocol?violation:?
"
?
+
?e.getMessage());
????????????e.printStackTrace();
????????}
?
catch
?(IOException?e)?
{
????????????System.err.println(
"
Fatal?transport?error:?
"
?
+
?e.getMessage());
????????????e.printStackTrace();
????????}
?
finally
?
{
????????????
//
?Release?the?connection.
????????????post_method.releaseConnection();
????????}
????????
????????
byte
[]?responseBody?
=
?
null
;
????????
????????GetMethod?get_method?
=
?
new
?GetMethod(
"
http://localhost/rcpq/unit.php
"
);
????????
????????
//
?Provide?custom?retry?handler?is?necessary
????????get_method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
????????????????
new
?DefaultHttpMethodRetryHandler(
3
,?
false
));
????????
????????
try
?
{
????????????
//
?Execute?the?method.
????????????
int
?statusCode?
=
?client.executeMethod(get_method);
????????????
????????????
if
?(statusCode?
!=
?HttpStatus.SC_OK)?
{
????????????????System.err.println(
"
Method?failed:?
"
?
+
?get_method.getStatusLine());
????????????}
????????????
????????????
//
?Read?the?response?body.
????????????
//
responseBody?=?get_method.getResponseBody();
????????????
//
這里用流來讀頁面
????????????
????????????InputStream?in?
=
?get_method.getResponseBodyAsStream();
????????????
if
?(in?
!=
?
null
)?
{
????????????????
byte
[]?tmp?
=
?
new
?
byte
[
4096
];
????????????????
int
?bytesRead?
=
?
0
;
????????????????ByteArrayOutputStream?buffer?
=
?
new
?ByteArrayOutputStream(
1024
);
????????????????
while
?((bytesRead?
=
?in.read(tmp))?
!=
?
-
1
)?
{
????????????????????buffer.write(tmp,?
0
,?bytesRead);
????????????????}
????????????????responseBody?
=
?buffer.toByteArray();
????????????}
????????????
????????????
????????????
//
?Deal?with?the?response.
????????????
//
?Use?caution:?ensure?correct?character?encoding?and?is?not?binary?data
????????????
//
System.out.println(new?String(responseBody));
????????????
????????}
?
catch
?(HttpException?e)?
{
????????????System.err.println(
"
Fatal?protocol?violation:?
"
?
+
?e.getMessage());
????????????e.printStackTrace();
????????}
?
catch
?(IOException?e)?
{
????????????System.err.println(
"
Fatal?transport?error:?
"
?
+
?e.getMessage());
????????????e.printStackTrace();
????????}
?
finally
?
{
????????????
//
?Release?the?connection.
????????????get_method.releaseConnection();
????????}
????????
????????Parser?parser;
????????
????????parser?
=
?Parser.createParser(
new
?String(responseBody,?
"
GBK
"
),?
"
GBK
"
);
????????
????????
????????String?filterStr?
=
?
"
table
"
;
????????NodeFilter?filter?
=
?
new
?TagNameFilter(filterStr);
????????
????????NodeList?tables?
=
?parser.extractAllNodesThatMatch(filter);
????????
????????
//
System.out.println(tables.elementAt(17).toString());
???????
//
找到單位列表所在的表格
????????
????????TableTag?tabletag?
=
?(TableTag)?tables.elementAt(
17
);
????????
????????TableRow?row?
=
?tabletag.getRow(
3
);
????????
????????TableColumn[]?cols?
=
?row.getColumns();
????????
//
System.out.println("單位名稱:"?+?cols[2].toHtml());
????????System.out.println(
"
單位名稱:
"
?
+
?cols[
2
].childAt(
0
).getText());
????????
????}
????
}
轉自:
http://blog.csdn.net/danny_xcz/archive/2007/01/19/1487602.aspx
posted on 2007-01-22 13:42
tambc
閱讀(791)
評論(0)
編輯
收藏
所屬分類:
Java
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
HttpClient和HtmlParser配合實現自動登陸系統抽取頁面信息
Acegi+hibernate 動態實現基于角色的權限管理
服務器是怎么要求客戶端強行彈出身份驗證窗口的
java的ubb類
java版的escape和unescape
用java實現漢字的筆畫數(轉貼)
用Yale CAS Server 來實現單點登陸(SSO)
實現HTTP長連接(服務器推)
Powered by:
BlogJava
Copyright © tambc
主站蜘蛛池模板:
枣庄市
|
东乌珠穆沁旗
|
吐鲁番市
|
宜良县
|
乌海市
|
藁城市
|
昂仁县
|
嘉祥县
|
汕头市
|
巴里
|
益阳市
|
富蕴县
|
佳木斯市
|
万山特区
|
吐鲁番市
|
台北县
|
临西县
|
平罗县
|
屯门区
|
三江
|
青海省
|
承德县
|
鄯善县
|
仁寿县
|
团风县
|
宁都县
|
玉树县
|
沅陵县
|
房山区
|
黄冈市
|
囊谦县
|
宁晋县
|
南澳县
|
镇雄县
|
通河县
|
进贤县
|
黔江区
|
青川县
|
革吉县
|
玉树县
|
波密县
|