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

相关文章
相关标签/搜索