httpclient的幾種請求URL的方式

來源:互聯網
上載者:User

標籤:使用方法   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的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.