1,HttpClient
利用apache的虛擬客戶端包獲取某個地址的內容
2.java自帶的HttpURLConnection
利用apache的虛擬客戶端包獲取某個地址的內容
?1
import?java.io.UnsupportedEncodingException;
?2
import?java.util.HashSet;
?3
import?java.util.Iterator;
?4
import?java.util.Set;
?5
import?java.util.regex.Matcher;
?6
import?java.util.regex.Pattern;
?7
?8
import?org.apache.commons.httpclient.HttpClient;
?9
import?org.apache.commons.httpclient.NameValuePair;
10
import?org.apache.commons.httpclient.methods.PostMethod;
11
12
public?class?catchMain?
{
13
14
????/**?*//**
15
?????*?@param?args
16
?????*/
17
????public?static?void?main(String[]?args)?
{
18
????????
19
20
????????String?url?=?"http://search.foodqs.com/companysearch.asp";
21
????????String?keyword="食";
22
????????String?response=createhttpClient(url,keyword);
23
????}
24
25
public?static?String?createhttpClient(String?url,String?param)
{
26
????????HttpClient?client?=?new?HttpClient();
27
????????String?response=null;
28
????????String?keyword=null;
29
????????PostMethod?postMethod?=?new?PostMethod(url);
30
????????try?
{
31
????????????if(param!=null)
32
???????????keyword?=?new?String(param.getBytes("gb2312"),"ISO-8859-1");
33
????????}?catch?(UnsupportedEncodingException?e1)?
{
34
????????????//?TODO?Auto-generated?catch?block
35
????????????e1.printStackTrace();
36
????????}
37
????????
38
????????NameValuePair[]?data?=?
{?new?NameValuePair("keyword",?keyword)?};
39
????????//?將表單的值放入postMethod中
40
????????postMethod.setRequestBody(data);
41
????????
42
????????try?
{
43
????????????int?statusCode?=?client.executeMethod(postMethod);
44
????????????response?=?new?String(postMethod.getResponseBodyAsString()
45
????????????????????.getBytes("ISO-8859-1"),?"GBK");
46
????????}?catch?(Exception?e)?
{
47
48
????????????e.printStackTrace();
49
????????}
50
????????return?response;
51
????????????
52
????}
53
????????

?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



31

32

33



34

35

36

37

38



39

40

41

42



43

44

45

46



47

48

49

50

51

52

53

2.java自帶的HttpURLConnection
?1
public?static?String?getPageContent(String?strUrl,?String?strPostRequest,
?2
????????????int?maxLength)?
{
?3
????????//讀取結果網頁
?4
????????StringBuffer?buffer?=?new?StringBuffer();
?5
????????System.setProperty("sun.net.client.defaultConnectTimeout",?"5000");
?6
????????System.setProperty("sun.net.client.defaultReadTimeout",?"5000");
?7
????????try?
{
?8
????????????URL?newUrl?=?new?URL(strUrl);
?9
????????????HttpURLConnection?hConnect?=?(HttpURLConnection)?newUrl
10
????????????????????.openConnection();
11
????????????//POST方式的額外數據
12
????????????if?(strPostRequest.length()?>?0)?
{
13
????????????????hConnect.setDoOutput(true);
14
????????????????OutputStreamWriter?out?=?new?OutputStreamWriter(hConnect
15
????????????????????????.getOutputStream());
16
????????????????out.write(strPostRequest);
17
????????????????out.flush();
18
????????????????out.close();
19
????????????}
20
????????????//讀取內容
21
????????????BufferedReader?rd?=?new?BufferedReader(new?InputStreamReader(
22
????????????????????hConnect.getInputStream()));
23
????????????int?ch;
24
????????????for?(int?length?=?0;?(ch?=?rd.read())?>?-1
25
????????????????????&&?(maxLength?<=?0?||?length?<?maxLength);?length++)
26
????????????????buffer.append((char)?ch);
27
????????????rd.close();
28
????????????hConnect.disconnect();
29
????????????return?buffer.toString().trim();
30
????????}?catch?(Exception?e)?
{
31
????????????//?return?"錯誤:讀取網頁失??!";
32
????????????return?null;
33
????????}
34
????}

?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



31

32

33

34

BufferedReader rd = new BufferedReader(new InputStreamReader( hConnect.getInputStream() ,encoding));