天天看點

httpclient的幾種請求URL的方式

一、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");
        //谷歌的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");
        //谷歌的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的使用方法了,當然在這個隻是簡單的例子,實際的還是要在具體的生産環境中自己封裝使用。