新引入Hutool-HttpUtil的使用(更簡單,更強大!),詳見:http://www.cnblogs.com/jiangbei/p/7667858.html
一、概述
1.簡介
根據凡技術必登其官網的原則(如果有),我們可以先訪問其官網:http://hc.apache.org/httpcomponents-client-4.5.x/index.html
摘取的簡介如下:
谷歌翻譯如下:
關于更加詳細的本地化的介紹,可以參見網友的博客:http://blog.csdn.net/wangpeng047/article/details/19624529/
簡單的來說,可以在Java代碼中使用httpClient來調用服務(HTTP協議),類似之前在Ajax中發送請求類似
2.相關教程
可以參考官網給出的教程:http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/index.html
二、入門
1.下載
這里采用的是maven進行構建,引入的依賴如下:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version>
</dependency>
?
當然,如果使用其它獲取方式(jar或者gradle等),可以參見官網相關介紹:
http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/dependency-info.html
2.起步
這里參考上文隨筆處的步驟與寫法,當然,對于官網有quick start的,還是有必要進行閱讀的:
http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html
使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
1.?創建HttpClient對象。
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.?釋放連接。無論執行方法是否成功,都必須釋放連接
1.發送無參GET請求
public static void main(String[] args) {// 創建httpClientCloseableHttpClient httpClient = HttpClients.createDefault();try {// 創建httpGet(請使用http://)HttpGet httpGet = new HttpGet("http://www.sogou.com");// 執行請求CloseableHttpResponse response = httpClient.execute(httpGet);try {// 處理響應HttpEntity entity = response.getEntity();// 響應狀態碼System.out.println("=================================");System.out.println(response.getStatusLine().getStatusCode());// 打印響應體(使用工具類)
System.out.println(EntityUtils.toString(entity));System.out.println("=================================");} finally { // 關閉response
response.close();}} catch (Exception e) {e.printStackTrace();} finally { // 關閉httpClienttry {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}
結果:
// 如果網頁內容亂碼,可以在toStrng位置加上編碼參數
2.發送帶參數的GET請求(通過HttpGet的URI參數的構造器)
public static void main(String[] args) {// 創建httpClientCloseableHttpClient httpClient = HttpClients.createDefault();try {// 創建URIBuilderURIBuilder uriBuilder = new URIBuilder("http://www.sogou.com");// 設置參數uriBuilder.addParameter("query", "more love");// 使用URIBuilder創建httpGetHttpGet httpGet = new HttpGet(uriBuilder.build());// 執行請求CloseableHttpResponse response = httpClient.execute(httpGet);try {// 處理響應HttpEntity entity = response.getEntity();// 響應狀態碼System.out.println("=================================");System.out.println(response.getStatusLine().getStatusCode());// 打印響應體(使用工具類)// System.out.println(EntityUtils.toString(entity));System.out.println("=================================");} finally { // 關閉response
response.close();}} catch (Exception e) {e.printStackTrace();} finally { // 關閉httpClienttry {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}
// 核心就是URIBuilder對URI的創建,響應處理不變
3.發送POST請求
public static void main(String[] args) {// 創建httpClientCloseableHttpClient httpClient = HttpClients.createDefault();try {HttpPost httpPost = new HttpPost("http://post_uri");List<NameValuePair> nvps = new ArrayList<>();nvps.add(new BasicNameValuePair("username", "vip"));nvps.add(new BasicNameValuePair("password", "secret"));// 同樣可以指定參數的編碼httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));// 執行請求CloseableHttpResponse response = httpClient.execute(httpPost);try {// 處理響應HttpEntity entity = response.getEntity();// 響應狀態碼System.out.println("=================================");System.out.println(response.getStatusLine().getStatusCode());// 打印響應體(使用工具類)// System.out.println(EntityUtils.toString(entity));System.out.println("=================================");} finally { // 關閉response
response.close();}} catch (Exception e) {e.printStackTrace();} finally { // 關閉httpClienttry {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}
?