2021-01-11 16:40:04
上篇文章给大家介绍了HttpClient详细使用示例详解,喜欢的朋友可以点击查看,今天继续给大家介绍HttpClient用法,具体内容如下所示;
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。
HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http协议的),提高了开发的效率,也方便提高代码的健壮性。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wo</groupId> <artifactId>HttpClient_test</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> </project>
@RequestMapping("findAll") public String findAll() throws Exception{ https://获得Http客户端 CloseableHttpClient build = HttpClientBuilder.create().build(); https://创建get请求 HttpGet httpGet = new HttpGet("http:https://localhost:8088/lunbo/findAll"); https://执行请求 CloseableHttpResponse execute = build.execute(httpGet); https://解析返回值 StatusLine statusLine = execute.getStatusLine(); https://获取到返回状态码 System.out.println("状态码为:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; }
https://post路径传参 @RequestMapping("/findAllPost/{page}/{size}") public String findAll(@PathVariable("page") int page,@PathVariable("size") int size) throws Exception { https://获得Http客户端 CloseableHttpClient build = HttpClientBuilder.create().build(); https://创建post请求 HttpPost httpPost = new HttpPost("http:https://localhost:8088/position/findAll/"+page+"/"+size); https://执行请求 CloseableHttpResponse execute = build.execute(httpPost); https://解析返回值 StatusLine statusLine = execute.getStatusLine(); https://获取到返回状态码 System.out.println("状态码为:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; } https://post map传参 @RequestMapping("findById") public String findById(@RequestParam("id") Integer id)throws Exception{ https://创建httpclicent请求对象 CloseableHttpClient build = HttpClientBuilder.create().build(); https://声明请求方式 HttpPost httpPost = new HttpPost("http:https://localhost:8088/position/findById"); https://声明携带参数 Map map=new HashMap<>(); map.put("id",id); https://将map转换为json格式 Object o = JSONObject.toJSON(map); https://设置请求 参数的编码格式 StringEntity stringEntity = new StringEntity(o.toString(), "utf-8"); https://将参数设置到请求对象中 httpPost.setEntity(stringEntity); https://设置content-Type httpPost.setHeader("Content-Type","application/json"); https://执行请求 CloseableHttpResponse execute = build.execute(httpPost); https://解析返回值 StatusLine statusLine = execute.getStatusLine(); https://获取到返回状态码 System.out.println("状态码为:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; }
原文链接:http://www.yuepc.com/a/1640.html
http://www.yuepc.com 为 “沈一博客” 唯一官方服务平台,请勿相信其他任何渠道。
2021-06-10
2021-05-19
2021-05-08
2021-06-08
2021-06-07
2021-05-08
2021-06-06
2021-05-21
2021-05-21
2021-05-18