Http協議非常的重要,HttpClient相比傳統JDK自帶的URLConnection,增加了易用性和靈活性(具體區別,日后我們再討論),它不僅是客戶端發送Http請求變得容易,而且也方便了開發人員測試接口(基於Http協議的),即提高了開發的效率,也方便提高代碼的健壯性。因此熟練掌握HttpClient是很重要的必修內容,掌握HttpClient后,相信對於Http協議的了解會更加深入。
一、Http簡介
HttpClient是Apache Jakarta Common下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。HttpClient已經應用在很多的項目中,比如Apache Jakarta上很著名的另外兩個開源項目Cactus和HTMLUnit都使用了HttpClient。
HTTP是一個屬於應用層的面向對象的協議,由於其簡捷、快速的方式,適用於分布式超媒體信息系統。它於1990年提出,經過幾年的使用與發展,得到不斷地完善和擴展。目前在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的規范化工作正在進行之中,而且HTTP-NG(Next Generation of HTTP)的建議已經提出。
HTTP協議的主要特點可概括如下:
1.支持客戶/服務器模式。
2.簡單快速:客戶向服務器請求服務時,只需傳送請求方法和路徑。請求方法常用的有GET、HEAD、POST。每種方法規定了客戶與服務器聯系的類型不同。
由於HTTP協議簡單,使得HTTP服務器的程序規模小,因而通信速度很快。
3.靈活:HTTP允許傳輸任意類型的數據對象。正在傳輸的類型由Content-Type加以標記。
4.無連接:無連接的含義是限制每次連接只處理一個請求。服務器處理完客戶的請求,並收到客戶的應答后,即斷開連接。采用這種方式可以節省傳輸時間。
5.無狀態:HTTP協議是無狀態協議。無狀態是指協議對於事務處理沒有記憶能力。缺少狀態意味着如果后續處理需要前面的信息,則它必須重傳,這樣可能導致每次連接傳送的數據量增大。另一方面,在服務器不需要先前信息時它的應答就較快。
二、使用方法
使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
首先需要導入HttpClientjar包,具體可以到官網下載,下載地址: http://hc.apache.org/downloads.cgi
commons-codec-1.7.jar,commons-logging-1.1.1.jar,httpclient-4.2.2.jar,httpcore-4.2.2.jar
1. 創建HttpClient對象,最新版的httpClient使用實現類的是closeableHTTPClient,以前的default作廢了。
2. 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。
4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
6. 釋放連接。無論執行方法是否成功,都必須釋放連接
三、使用實例
3.1.HttpClient提交表單模擬用戶登錄
1 package service; 2 import java.io.IOException; 3 import java.io.UnsupportedEncodingException; 4 import java.util.ArrayList; 5 import java.util.List; 6 import org.apache.http.HttpEntity; 7 import org.apache.http.NameValuePair; 8 import org.apache.http.client.ClientProtocolException; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.CloseableHttpResponse; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.impl.client.CloseableHttpClient; 13 import org.apache.http.impl.client.HttpClients; 14 import org.apache.http.message.BasicNameValuePair; 15 import org.apache.http.util.EntityUtils; 16 17 public class ServiceHttpImpl implements ServiceHttp { 18 /** 19 * post方式提交表單,模擬用戶登錄請求 20 */ 21 public void Login(String username, String password) { 22 //1.創建默認的httpclient實例 23 CloseableHttpClient httpclient = HttpClients.createDefault(); 24 String url = "http://localhost:8080/httpcillent/HttpCilentTest1?username=" 25 + username + "&password=" + password; 26 //2.創建httpPost 27 HttpPost httpPost = new HttpPost(url); 28 //3.創建參數隊列 29 List<NameValuePair> list = new ArrayList<NameValuePair>(); 30 list.add(new BasicNameValuePair("username", username)); 31 list.add(new BasicNameValuePair("password", password)); 32 /** 33 * UrlEncodedFormEntity這個類是用來把輸入數據編碼成合適的內容 34 *兩個鍵值對,被UrlEncodedFormEntity實例編碼后變為如下內容: 35 * key1=value1&key2=value2的形式 36 */ 37 UrlEncodedFormEntity entity; 38 CloseableHttpResponse response=null; 39 try { 40 entity=new UrlEncodedFormEntity(list,"utf-8"); 41 httpPost.setEntity(entity);//帶上參數執行 42 System.out.println("執行請求:"+httpPost.getURI()); 43 try { 44 response=httpclient.execute(httpPost);//響應結果 45 HttpEntity httpEntity=response.getEntity(); 46 if(httpEntity!=null){ 47 System.out.println("-----------------"); 48 System.out.println(EntityUtils.toString(httpEntity)); 49 System.out.println("-----------------"); 50 } 51 } catch (ClientProtocolException e) { 52 e.printStackTrace(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } catch (UnsupportedEncodingException e) { 57 e.printStackTrace(); 58 }finally{ 59 ServiceHttpImpl.CloseableHttpClient(httpclient); 60 ServiceHttpImpl.CloseableHttpResponse(response); 61 } 62 } 63 64 private static void CloseableHttpResponse(CloseableHttpResponse httpResponse) { 65 if (httpResponse != null) { 66 try { 67 httpResponse.close(); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 } 73 74 private static void CloseableHttpClient(CloseableHttpClient client) { 75 if (client != null) { 76 try { 77 client.close(); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 } 81 } 82 } 84 85 }
4.2 HTTP實體的使用
因為一個實體既可以代表二進制內容又可以代表字符內容,它也支持字符編碼(支持后者也就是字符內容)。實體是當使用封閉內容執行請求,或當請求已經成功執行,或當響應體結果發功到客戶端時創建的。
要從實體中讀取內容,可以通過HttpEntity#getContent()方法從輸入流中獲取,這會返回一個java.io.InputStream對象,或者提供一個輸出流到HttpEntity#writeTo(OutputStream)方法中,這會一次返回所有寫入到給定流中的內容。
當實體通過一個收到的報文獲取時,HttpEntity#getContentType()方法和HttpEntity#getContentLength()方法可以用來讀取通用的元數據,如Content-Type和Content-Length頭部信息(如果它們是可用的)。因為頭部信息Content-Type可以包含對文本MIME類型的字符編碼,比如text/plain或text/html,HttpEntity#getContentEncoding()方法用來讀取這個信息。如果頭部信息不可用,那么就返回長度-1,而對於內容類型返回NULL。如果頭部信息Content-Type是可用的,那么就會返回一個Header對象。當為一個傳出報文創建實體時,這個元數據不得不通過實體創建器來提供。
4.3發送post請求
public void testPost() { // 創建默認的httpClient實例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 創建httppost HttpPost httppost = new HttpPost("http://localhost:8080/"); // 創建參數隊列 List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("admin", "我心自在")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(list, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } }
測試結果如下:

4.3發送get請求
public void testGet() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 創建httpget. HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("executing request " + httpget.getURI()); // 執行get請求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 獲取響應實體 HttpEntity entity = response.getEntity(); System.out.println("--------------------------------------"); // 打印響應狀態 System.out.println(response.getStatusLine()); if (entity != null) { // 打印響應內容長度 System.out.println("Response content length: " + entity.getContentLength()); // 打印響應內容 System.out.println("Response content: " + EntityUtils.toString(entity)); } System.out.println("------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } }
測試結果如下:

總結httpGet和httpPost的區別和聯系:
HttpClient常用HttpGet和HttpPost這兩個類,分別對應Get方式和Post方式。
HttpPost方法提交HTTP POST請求,需要使用HttpPost類的setEntity方法設置請求參數。參數則必須用NameValuePair[]數組存儲。
表單提交中的post和get方法的區別:
2. get是把參數數據隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個字段一一對應,在URL中可以看到。post是通過HTTP post機制,將表單內各個字段與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。用戶看不到這個過程。
3. 對於get方式,服務器端用Request.QueryString獲取變量的值,對於post方式,服務器端用Request.Form獲取提交的數據。
4. get傳送的數據量較小,不能大於2KB。post傳送的數據量較大,一般被默認為不受限制。但理論上,IIS4中最大量為80KB,IIS5中為100KB。
5. get安全性非常低,傳輸數據可見,post安全性較高,傳輸數據不可見,但通過抓包工具post傳遞中的參數也可以看到,所以理論上也不是安全的。
6. get是Form的默認方法。
