1.1 執行請求
HttpClient最重要的功能就是執行HTTP方法。HTTP方法的執行涉及到一個或幾個HTTP請求和響應的交換,這通常在HttpClient內部處理。用戶提供執行的請求對象,HttpClient傳輸此請求到目標服務器並返回對應的響應對象或者在執行不成的情況下拋出異常。
很自然地,HttpClientAPI的主要入口點是定義了上述描述的協議的HttpClient接口。
下面是一個簡單的執行請求過程的示例:
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try { <...> } finally { response.close(); }
1.1.1 HTTP請求
所有的HTTP請求都有一個包含方法名稱、請求URI和一個HTTP協議版本的請求線。HttpClient支持所有HTTP/1.1定義的方法,這些方法是:GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS。在HttpClient中和這些方法對應的類分別爲HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace,HttpOptions。
URI(統一資源標識符)標記了一個可被請求的資源,HTTP請求URIs由協議概述、主機名稱、資源路徑、以及其他可選的(端口、查詢參數)參數組成。
HttpGet httpget = new HttpGet("http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient提供URIBuilder工具類簡化請求URIs的創建。
URI uri = new URIBuilder() .setScheme("http") .setHost("www.google.com") .setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", "") .build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
輸出:
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
1.1.2 HTTP響應
HTTP響應是在服務器接受並解析請求消息後返回給客戶端的信息。信息的第一行由協議版本
、數字表示的狀態碼以及文本表示的狀態組成。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString());
輸出:
HTTP/1.1 200 OK HTTP/1.1 200 OK
1.1.3 消息頭
一個HTTP消息可以包含一些描述諸如消息長度、消息類型等的消息屬性的頭部信息,HttpClient
提供一些獲取、添加、刪除以及列舉頭部信息的方法。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); Header h1 = response.getFirstHeader("Set-Cookie"); System.out.println(h1); Header h2 = response.getLastHeader("Set-Cookie"); System.out.println(h2); Header[] hs = response.getHeaders("Set-Cookie"); System.out.println(hs.length);
輸出:
Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost" 2
最快的獲取所有消息頭部信息的方式是使用HeaderIterator接口
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie","c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie","c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderIterator it = response.headerIterator("Set-Cookie"); while (it.hasNext()) { System.out.println(it.next()); }
輸出:
Set-Cookie: c1=a; path=/; domain=localhost Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
HttpClient也提供了方便的方法把HTTP頭部消息解析成單個的元素。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, "OK"); response.addHeader("Set-Cookie","c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie","c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie")); while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i < params.length; i++) { System.out.println(" " + params[i]); } }
輸出:
c1 = a path=/ domain=localhost c2 = b path=/ c3 = c domain=localhost