httpClient工具介紹
HTTP協議可能是現在lntemet上使用得最多、最重要的協議了,越來越多的Java應用程序需要直接通過HTTP協議來訪問網絡資源。雖然在JDK的java.net包中已經提供了訪問http協議的基木功能,但是對於大部分應用程序來說,JDK庫本身提供的功能還不夠豐富和靈活。HttpClient是ApaChe、JakamComnmn下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP協議的客戶端編程工具包,並且它支持HTTP辦議最新的版術和建議。
httpClient工具的安裝
所需要依賴的jar包是以下三個:
http-core-4.4.6.jar、commons-logging-1.2.jar、commons-codec-1.10.jar
方法一:到Apache官網下載工具包,然后導入到eclipse中即可,下載地址:http://hc.apache.org/downloads.cgi
方法二:前提條件是:eclipse要配置好Maven,在工程中的pom.xml文件中,寫入代碼,保存便自動下載jar包。搜索httpclient地址:http://www.mvnrepository.com/,操作如下所示:
①點擊網址,進入首頁,在搜索框中輸入httpclient搜索
②點擊搜索出來的第一個Apache HttpClient
③選擇不同的版本,點擊(本文以4.5.3版本為例)
④將上圖紅色框中的代碼復制到eclipse建的Maven工程中的pom.xml文件下,操作如下圖所示:
⑤保存pom.xml文件后,便會自動下載jar包,可在Maven工具包中查看到
httpClient工具的主要功能
1.實現了所有HTTP的方法(get、post、put、head等)
2.支持自動轉向
3.支持代理服務器
4.自動處理set-cookie中的cookie
5.直接獲取服務器發送的response code和headers
6.設置連接超時的能力
需要注意的是,httpclient版本的不同,功能實現的代碼也就不同,需要謹慎。
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. 釋放連接。 無論執行方法是否成功, 都必須釋放連接。
說了這么多隨處可見的東西,來兩個實例,分別是get和post請求操作
get實例,url地址為百度地址,代碼如下:
1 import java.io.IOException; 2 3 import org.apache.http.HttpEntity; 4 import org.apache.http.client.ClientProtocolException; 5 import org.apache.http.client.methods.CloseableHttpResponse; 6 import org.apache.http.client.methods.HttpGet; 7 import org.apache.http.impl.client.CloseableHttpClient; 8 import org.apache.http.impl.client.HttpClients; 9 import org.apache.http.util.EntityUtils; 10 11 public class yihuqingjiu_get { 12 13 public static void main(String[] args) throws ClientProtocolException, IOException{ 14 //創建httpclient對象 15 CloseableHttpClient httpClient = HttpClients.createDefault(); 16 //創建請求方法的實例, 並指定請求url 17 HttpGet httpget=new HttpGet("http://www.baidu.com"); 18 //獲取http響應狀態碼 19 CloseableHttpResponse response=httpClient.execute(httpget); 20 HttpEntity entity=response.getEntity(); 21 //接收響應頭 22 String content=EntityUtils.toString(entity, "utf-8"); 23 System.out.println(httpget.getURI()); 24 System.out.println(content); 25 httpClient.close(); 26 } 27 28 }
控制台顯示結果如下圖所示:
帶參數的post實例,url地址為禪道地址,代碼如下:
1 package com.httpclient; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.http.HttpEntity; 8 import org.apache.http.NameValuePair; 9 import org.apache.http.client.ClientProtocolException; 10 import org.apache.http.client.entity.UrlEncodedFormEntity; 11 import org.apache.http.client.methods.CloseableHttpResponse; 12 import org.apache.http.client.methods.HttpPost; 13 import org.apache.http.impl.client.CloseableHttpClient; 14 import org.apache.http.impl.client.HttpClients; 15 import org.apache.http.message.BasicNameValuePair; 16 import org.apache.http.util.EntityUtils; 17 18 public class yihuqingjiu_post_1 { 19 20 public static void main(String[] args) throws ClientProtocolException, IOException { 21 22 CloseableHttpClient httpClient = HttpClients.createDefault(); 23 HttpPost httpPost = new HttpPost("http://127.0.0.1:81/zentao/user-login-L3plbnRhby8=.html"); 24 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 25 nameValuePairs.add(new BasicNameValuePair("account", "admin")); 26 nameValuePairs.add(new BasicNameValuePair("password", "LHongboss941025")); 27 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 28 CloseableHttpResponse response = httpClient.execute(httpPost); 29 HttpEntity entity = response.getEntity(); 30 String content = EntityUtils.toString(entity, "utf-8"); 31 System.out.println(content); 32 System.out.println(httpPost.getURI()); 33 System.out.println(response); 34 httpClient.close(); 35 36 } 37 38 }
控制台顯示結果如下圖所示:
帶參數的get請求,接口使用的是豆瓣圖書,代碼如下:
1 import java.io.IOException; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import org.apache.http.Consts; 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.HttpGet; 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 yihuqingjiu_get_1 { 18 19 public static void main(String[] args) throws ClientProtocolException, IOException { 20 //創建httpclient對象 21 CloseableHttpClient httpClient = HttpClients.createDefault(); 22 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 23 nameValuePairs.add(new BasicNameValuePair("q", "瓦爾登湖")); 24 String str = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, 25 Consts.UTF_8)); 26 HttpGet httpGet = new HttpGet("https://api.douban.com/v2/book/search"+"?"+str); 27 CloseableHttpResponse response=httpClient.execute(httpGet); 28 HttpEntity entity=response.getEntity(); 29 //接收響應頭 30 String content=EntityUtils.toString(entity, "utf-8"); 31 System.out.println(content); 32 httpClient.close(); 33 34 } 35 36 }
控制台顯示結果:
這樣顯示看不出什么效果,需要使用json解析工具格式化就可以,解析工具地址:http://www.bejson.com/,操作如下所示:
①進入網址,點擊JSON相關,選擇JSON視圖
②將eclipse控制台的信息復制到輸入框中,並點擊格式化
③點擊格式化之后,再點擊視圖,即可出現如下界面,可清晰查看信息