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的使用方法了,当然在这个只是简单的例子,实际的还是要在具体的生产环境中自己封装使用。

 

上一篇:mysql慢查询


下一篇:作为 Java 开发程序员,你知道什么是 Serveless 架构吗?(上)