配置httpclient

时间:2019-10-20
本文章向大家介绍配置httpclient,主要包括配置httpclient使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在分布式项目中,前端系统需要调用后台的数据并初始化时,可以使用httpclient通信器。先做一个测试

一、导入需要的依赖

<!-- httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
         版本为4.5.5
</dependency>

后台系统为manage_taotao,前端系统为web_taotao,设置的域名分别为manage.taotao.com 和web.taotao.com。在后台的一个test中写一个httpclient get请求

@Test
    public void getTestOne() {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet("http://localhost:8001/rest/test/doGetControllerOne");//controller层的测试地址
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

写一个post请求

@Test
    public void doPostTest() {
        CloseableHttpClient httpClient=HttpClientBuilder.create().build();
        try {
            URI uri=new URIBuilder("http://localhost:8001/rest/test/doGetControllerTwo").addParameter("name", "陈一峰")
                    .addParameter("age", "18").build();
            HttpPost post=new HttpPost(uri);
            CloseableHttpResponse response = httpClient.execute(post);
         // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                    System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                    System.out.println("响应内容为:" + EntityUtils.toString(responseEntity,"utf-8"));
            }
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

json格式的post请求

@Test
    public void doPostEntityTest() {
        ObjectMapper mapper=new ObjectMapper();
        CloseableHttpClient httpClient=HttpClientBuilder.create().build();
        try {
            URI uri=new URIBuilder("http://manage.taotao.com/rest/test/doPostEntity").build();
            
            HttpPost post=new HttpPost(uri);
            User user=new User();
            user.setName("陈一峰");
            user.setAge(24);
            user.setSex(1);
            HttpEntity entity=new StringEntity(mapper.writeValueAsString(user),"utf-8");
            post.setHeader("Content-Type","application/json;charset=utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response = httpClient.execute(post);
         // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                    System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                    System.out.println("响应内容为:" + EntityUtils.toString(responseEntity,"utf-8"));
            }
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这里controller层代码

@RequestMapping("doPostEntity")
    @ResponseBody
    public String test3(@RequestBody User user) {
        System.out.println(user);
        return "success";
    }

原文地址:https://www.cnblogs.com/psxfd4/p/11708424.html