httpclient的幾種請求URL的方式

1、httpclient項目有兩種使用方式。一種是commons項目,這一個就只更新到3.1版本了。如今挪到了HttpComponents子項目下了,這裏重點講解HttpComponents下面的httpclient的使用方式。apache

2、加入jar包json

  <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.2</version>
    </dependency>

3、使用方式app

一、GET方法post

     //相對於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();

二、POST方法spa

     //須要傳輸的數據
        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();

三、PUT方式(和post的方式差很少)code

     //須要傳輸的數據
        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();

四、DELETE方法(這種方式和get方式差很少,可是限定類型不同)component

     //相對於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();

4、這基本上就是httpclient的使用方法了,固然在這個只是簡單的例子,實際的仍是要在具體的生產環境中本身封裝使用。blog

相關文章
相關標籤/搜索