vickzhu
BlogJava
::
首頁(yè)
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
151 隨筆 :: 0 文章 :: 34 評(píng)論 :: 0 Trackbacks
<
2008年11月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(6)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2018年10月 (1)
2014年8月 (1)
2014年7月 (1)
2013年11月 (1)
2013年3月 (1)
2012年6月 (2)
2011年9月 (1)
2011年8月 (2)
2011年7月 (1)
2010年12月 (2)
2010年8月 (2)
2010年7月 (1)
2010年5月 (1)
2010年4月 (2)
2010年3月 (2)
2010年1月 (1)
2009年12月 (2)
2009年11月 (5)
2009年10月 (1)
2009年9月 (1)
2009年7月 (4)
2009年6月 (2)
2009年5月 (3)
2009年4月 (3)
2009年3月 (9)
2009年2月 (3)
2009年1月 (3)
2008年12月 (1)
2008年11月 (5)
2008年10月 (6)
2008年9月 (7)
2008年8月 (8)
搜索
最新評(píng)論
1.?re: getOutputStream() has already been called for this response 錯(cuò)誤解決[未登錄]
asda
--s
2.?re: getOutputStream() has already been called for this response 錯(cuò)誤解決
3q問(wèn)題解決了
--lpy
3.?re: getOutputStream() has already been called for this response 錯(cuò)誤解決
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--jsp
4.?re: eclipse dynamic web project pom.xml配置
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--最代碼
5.?re: getOutputStream() has already been called for this response 錯(cuò)誤解決
@hhh
去年response.reset();即可
--lmf
閱讀排行榜
1.?getOutputStream() has already been called for this response 錯(cuò)誤解決(37279)
2.?正確、合理配置tomcat內(nèi)存-java.lang.OutOfMemoryError: PermGen space及其解決方法 (7091)
3.?android AlertDialog 動(dòng)態(tài)添加按鈕(7037)
4.?eclipse dynamic web project pom.xml配置(6460)
5.?導(dǎo)致Response commit的原因(5907)
評(píng)論排行榜
1.?getOutputStream() has already been called for this response 錯(cuò)誤解決(19)
2.?windows 下 Tomcat Apache 整合 (原創(chuàng))(2)
3.?java 讀取頁(yè)面源碼 的多種方式(2)
4.?org.hibernate.tuple.AbstractEntityTuplizer.createProxy(2)
5.?java 中的文本換行符(2)
java 讀取頁(yè)面源碼 的多種方式
以下都是實(shí)戰(zhàn)經(jīng)驗(yàn):
1、Socket讀取
String strServer=
http://www.google.cn
;//這里同樣可以用ip來(lái)訪問(wèn):203.208.35.100
String strPage="/language_tools?hl=zh-CN";
try {
String hostname = strServer;
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("GET " + strPage + " HTTP/1.0\r\n");
wr.write("HOST:" + strServer + "\r\n");
wr.write("\r\n");
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
System.out.println(e.toString());
}
2、HttpClient方式
HttpClient client=new HttpClient();
GetMethod method=new GetMethod("
int status=client.executeMethod(method);
if(status==HttpStatus.SC_OK){
//讀取內(nèi)容
byte[] responseBody = method.getResponseBody();
//處理內(nèi)容
System.out.println(new String(responseBody));
System.out.println("文件名稱:"+method.getPath());
}
3、HttpURLConnection方式
URL url = new URL("這里是你要連接的地址");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);//是否可用于輸出(輸出參數(shù)),默認(rèn)為fasle。另:setDoInput()為是否可用于輸入,默認(rèn)為true
String parameters = "name=admin&password=123456";//這里是要傳遞的參數(shù)
OutputStream os = conn.getOutputStream();
os.write(parameters.getBytes("utf-8"));
os.flush();
os.close();
System.out.println("返回狀態(tài)碼:"+conn.getResponseCode());
System.out.println("返回消息:"+conn.getResponseMessage());
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}
// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;
// DocumentBuilder db = dbf.newDocumentBuilder();
// Document doc = db.parse(is);
如果誰(shuí)還有更多的方式分享,請(qǐng)留言!
posted on 2008-11-12 10:12
筱 筱
閱讀(993)
評(píng)論(2)
編輯
收藏
評(píng)論
#
re: java 讀取頁(yè)面源碼 的多種方式[未登錄]
2008-11-24 20:39
fisher
GetMethod method=new GetMethod("
http://www.baidu.com/"
);
GetMethod ?
這個(gè)東西哪來(lái)的?
回復(fù)
更多評(píng)論
#
re: java 讀取頁(yè)面源碼 的多種方式
2008-11-25 09:03
vickzhu
你好HttpClient和GetMethod都是來(lái)自apache的包
回復(fù)
更多評(píng)論
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
Powered by:
BlogJava
Copyright © 筱 筱
主站蜘蛛池模板:
满洲里市
|
修文县
|
巴马
|
商水县
|
武鸣县
|
弋阳县
|
新干县
|
松潘县
|
抚远县
|
府谷县
|
米泉市
|
铜陵市
|
资阳市
|
杭锦后旗
|
恩平市
|
通海县
|
延安市
|
宣化县
|
马关县
|
瑞昌市
|
新田县
|
大余县
|
五家渠市
|
峨眉山市
|
通州市
|
松原市
|
称多县
|
丹凤县
|
五大连池市
|
育儿
|
武威市
|
义马市
|
上饶县
|
长白
|
舒城县
|
香河县
|
东方市
|
札达县
|
山阴县
|
保靖县
|
珠海市
|