標籤:使用方法 class new delete object 擷取 str 封裝 nts
一、httpclient項目有兩種使用方式。一種是commons項目,這一個就只更新到3.1版本了。現在挪到了HttpComponents子項目下了,這裡重點講解HttpComponents下面的httpclient的使用方式。
二、加入jar包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
三、使用方式
1、GET方法
//相對於commons-httpclient 3.1這裡採用介面的方式來擷取httpclient了 HttpClient httpClient = HttpClients.createDefault(); //聲明請求方式 HttpGet httpGet = new HttpGet("http://www.baidu.com"); //擷取相應資料,這裡可以擷取相應的資料 HttpResponse httpResponse = httpClient.execute(httpGet); //拿到實體 HttpEntity httpEntity= httpResponse.getEntity(); //擷取結果,這裡可以正對相應的資料精細字元集的轉碼 String result = ""; if (httpEntity != null) { result = EntityUtils.toString(httpEntity,"utf-8"); } //關閉串連 httpGet.releaseConnection();
2、POST方法
//需要傳輸的資料 Map<String,Object> map = new HashMap<String, Object>(); map.put("1", "1"); map.put("2", "2"); //Google的Gson Gson gson = new Gson(); //相對於commons-httpclient 3.1這裡採用介面的方式來擷取httpclient了 HttpClient httpClient = HttpClients.createDefault(); //聲明請求方式 HttpPost httpPost = new HttpPost("http://www.baidu.com"); //設定訊息頭 httpPost.setHeader("Content-Type","application/json;charset=utf-8"); httpPost.setHeader("Accept","application/json"); //設定發送資料(資料盡量為json),可以設定資料的發送時的字元集 httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8")); //擷取相應資料,這裡可以擷取相應的資料 HttpResponse httpResponse = httpClient.execute(httpPost); //拿到實體 HttpEntity httpEntity= httpResponse.getEntity(); //擷取結果,這裡可以正對相應的資料精細字元集的轉碼 String result = ""; if (httpEntity != null) { result = EntityUtils.toString(httpEntity,"utf-8"); } //關閉串連 httpPost.releaseConnection();
3、PUT方式(和post的方式差不多)
//需要傳輸的資料 Map<String,Object> map = new HashMap<String, Object>(); map.put("1", "1"); map.put("2", "2"); //Google的Gson Gson gson = new Gson(); //相對於commons-httpclient 3.1這裡採用介面的方式來擷取httpclient了 HttpClient httpClient = HttpClients.createDefault(); //聲明請求方式 HttpPut httpPut = new HttpPut("http://www.baidu.com"); //設定訊息頭 httpPut.setHeader("Content-Type","application/json;charset=utf-8"); httpPut.setHeader("Accept","application/json"); //設定發送資料(資料盡量為json),可以設定資料的發送時的字元集 httpPut.setEntity(new StringEntity(gson.toJson(map),"utf-8")); //擷取相應資料,這裡可以擷取相應的資料 HttpResponse httpResponse = httpClient.execute(httpPut); //拿到實體 HttpEntity httpEntity= httpResponse.getEntity(); //擷取結果,這裡可以正對相應的資料精細字元集的轉碼 String result = ""; if (httpEntity != null) { result = EntityUtils.toString(httpEntity,"utf-8"); } //關閉串連 httpPut.releaseConnection();
4、DELETE方法(這種方式和get方式差不多,但是限定類型不一樣)
//相對於commons-httpclient 3.1這裡採用介面的方式來擷取httpclient了 HttpClient httpClient = HttpClients.createDefault(); //聲明請求方式 HttpDelete httpDelete = new HttpDelete("http://www.baidu.com"); //設定訊息頭(這裡可以根據自己的介面來設定訊息頭) httpDelete.setHeader("Content-Type","application/json;charset=utf-8"); httpDelete.setHeader("Accept","application/json"); //擷取相應資料,這裡可以擷取相應的資料 HttpResponse httpResponse = httpClient.execute(httpDelete); //拿到實體 HttpEntity httpEntity= httpResponse.getEntity(); //擷取結果,這裡可以正對相應的資料精細字元集的轉碼 String result = ""; if (httpEntity != null) { result = EntityUtils.toString(httpEntity,"utf-8"); } //關閉串連 httpDelete.releaseConnection();
四、這基本上就是httpclient的使用方法了,當然在這個只是簡單的例子,實際的還是要在具體的生產環境中自己封裝使用。
httpclient的幾種請求URL的方式