Apache HttpClient 4.x 使用 GET, POST 範例

先前我曾經發了一篇介紹如何抓取網頁的教學:
利用Jakarta.Commons.HttpClient抓取網頁、網站(Parser),不過當時用的是 HttpClient 3.1。
經過時間的演進 Apache 已經在 14 August 2009 發佈HttpComponents HttpClient 4.0 (GA)
由 3.1 到 4.0 因為底層幾乎全部重新改寫,所以也使有些舊的程式無法使用。
這篇就是我自己寫的一個簡單範例。

在看範例之前先把一些重要連結整理給大家:
想知道這次到底更動了哪些東西可以看:Apache HttpClient 首頁
官方的 Tutorial 在:Apache HttpClient Tutorial
而 API DOC、說明文件則在:Apache HttpClient apidocs

相關的程式碼、jar 檔在:HttpComponents HttpClient 4.0 (GA)
注意,在寫程式前必需先將四個 jar 檔正確匯入,最後兩個(*)是選用,
請參考:http://hc.apache.org/httpcomponents-client/quickstart.html
  • commons-logging-x.x.x.jar
  • commons-codec-x.x.x.jar
  • httpcore-x.x.x.jar
  • httpclient-x.x.x.jar
  • apache-mime4j-x.x.x.jar (*)
  • httpmime-x.x.x.jar (*)
說了這麼多,以下是程式的範例,
第一個是傳回在 google 查詢 httpclient 的結果。
第二則是傳回台大圖書館查詢 Head First Java 的結果。

 1 package demo.httpclient;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 
 6 import org.apache.commons.httpclient.HttpStatus;
 7 import org.apache.http.HttpResponse;
 8 import org.apache.http.NameValuePair;
 9 import org.apache.http.client.methods.HttpGet;
10 import org.apache.http.client.methods.HttpPost;
11 import org.apache.http.client.utils.URLEncodedUtils;
12 import org.apache.http.entity.StringEntity;
13 import org.apache.http.impl.client.DefaultHttpClient;
14 import org.apache.http.message.BasicNameValuePair;
15 import org.apache.http.util.EntityUtils;
16 
17 /**
18 * Apache HttpClient 4.x 使用 GET, POST 查詢網頁的範例
19 *
20 * @author werdna at http://werdna1222coldcodes.blogspot.com/
21 */
22 
23 public class HttpClientDemo extends DefaultHttpClient {
24 
25     public static void main(String[] args) throws IOException {
26 
27         DefaultHttpClient demo = new DefaultHttpClient();
28         demo.getParams().setParameter("http.protocol.content-charset", "UTF-8");
29 
30         // Get Request Example,取得 google 查詢 httpclient 的結果
31         HttpGet httpGet = new HttpGet("http://www.google.com.tw/search?q=httpclinet");
32         HttpResponse response = demo.execute(httpGet);
33         String responseString = EntityUtils.toString(response.getEntity());
34         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
35             // 如果回傳是 200 OK 的話才輸出
36             System.out.println(responseString);
37         } else {
38             System.out.println(response.getStatusLine());
39         }
40 
41         // Post Request Example,查詢台大圖書館書籍
42         ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();
43         pairList.add(new BasicNameValuePair("searchtype", "t"));
44         pairList.add(new BasicNameValuePair("searchscope", "keyword"));
45         pairList.add(new BasicNameValuePair("searcharg", "Head First Java"));
46         pairList.add(new BasicNameValuePair("SORT", "D"));
47 
48         HttpPost httpPost = new HttpPost("http://tulips.ntu.edu.tw:1081/search*cht/a?");
49         StringEntity entity = new StringEntity(URLEncodedUtils.format(pairList, "UTF-8"));
50         httpPost.setEntity(entity);
51         response = demo.execute(httpPost);
52         responseString = EntityUtils.toString(response.getEntity());
53         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
54             // 如果回傳是 200 OK 的話才輸出
55             System.out.println(responseString);
56         } else {
57             System.out.println(response.getStatusLine());
58         }
59     }
60 }

更多的程式範例可以參考其教學: 
HttpClient 4 HTTP request 
HttpClient 4 使用POST方式提交普通表单数据的例子

關鍵字:Apache、HttpClient、4、教學、示範、範例、設定
參考資料:
  1. Apache HttpClient 首頁
  2. HttpComponents HttpClient 4.0 (GA)
  3. Apache HttpClient Tutorial
  4. Apache HttpClient apidocs
  5. HttpClient 4 HTTP request
  6. HttpClient 4 使用POST方式提交普通表单数据的例子 

留言

這個網誌中的熱門文章

【博客來折價券】博客來免費序號e-coupon分享(持續更新)

【博客來折價券】25/50/100/150現領現折+天天簽到換200+OP兩倍換!

【銀行代碼查詢】3碼銀行代碼列表、7碼分行代碼查詢

【新光卡攻略】台灣Pay單筆888送50+寰宇3%,LINE會員繳費10%!

【Hami Video】免費Hami Video體驗/試用序號分享(隨時更新)!

Gmail 無限分身術:產生無限 email 帳號