1,HttpClient
利用apache的虛擬客戶端包獲取某個(gè)地址的內(nèi)容
?1import?java.io.UnsupportedEncodingException;
?2import?java.util.HashSet;
?3import?java.util.Iterator;
?4import?java.util.Set;
?5import?java.util.regex.Matcher;
?6import?java.util.regex.Pattern;
?7
?8import?org.apache.commons.httpclient.HttpClient;
?9import?org.apache.commons.httpclient.NameValuePair;
10import?org.apache.commons.httpclient.methods.PostMethod;
11
12public?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
25public?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.java自帶的HttpURLConnection


?1public?static?String?getPageContent(String?strUrl,?String?strPostRequest,
?2????????????int?maxLength)?{
?3????????//讀取結(jié)果網(wǎng)頁
?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方式的額外數(shù)據(jù)
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????????????//讀取內(nèi)容
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?"錯(cuò)誤:讀取網(wǎng)頁失敗!";
32????????????return?null;
33????????}

34????}