169
技術社區[雲棲]
《HttpClient官方文檔》1.1 執行請求(一)
1.1. 執行請求
HttpClient最基本的功能就是執行HTTP方法。 一個HTTP方法的執行包含一次或多次HTTP請求與響應,通常由HttpClient的內部處理。
用戶提供一個請求對象,HttpClient發送該請求到目標服務器,服務器返回相應的響應對象,如果執行未成功則拋出一個異常。
很自然地,HttpClient的API的主要入口點就是定義了上述協議的HttpClient接口。下麵是一個最簡單的請求執行過程例子
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("https://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try { <...> } finally { response.close(); }
所有HTTP請求都有由方法名,請求URI和HTTP協議版本組成的請求行。
HttpClient支持開箱即用HTTP/1.1規範中定義的所有HTTP方法:GET
, HEAD
,POST
, PUT
, DELETE
,TRACE
and OPTIONS
。它們都有一個特定的類對應這些方法類型: HttpGet
,HttpHead
, HttpPost
,HttpPut
, HttpDelete
,HttpTrace
, and HttpOptions
.
請求的URI是統一資源定位符,它標識了應用於哪個請求之上的資源。HTTP請求的URI包含協議方案,主機名,可選的端口,資源路徑,可選查詢和可選片段。
HttpGet httpget = new HttpGet( "https://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient 提供 URIBuilder
實用類來簡化請求 URL的創建和修改.
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());
輸出內容為 >
https://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
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
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"
它還提供了方便的方法來解析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
最後更新:2017-05-19 13:32:30