时间:2021-06-16 08:22:59 | 栏目:JAVA代码 | 点击:次
上篇文章给大家介绍了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://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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{
//获得Http客户端
CloseableHttpClient build = HttpClientBuilder.create().build();
//创建get请求
HttpGet httpGet = new HttpGet("http://localhost:8088/lunbo/findAll");
//执行请求
CloseableHttpResponse execute = build.execute(httpGet);
//解析返回值
StatusLine statusLine = execute.getStatusLine();
//获取到返回状态码
System.out.println("状态码为:"+statusLine.getStatusCode());
String s = EntityUtils.toString(execute.getEntity());
build.close();
execute.close();
return s;
}
//post路径传参
@RequestMapping("/findAllPost/{page}/{size}")
public String findAll(@PathVariable("page") int page,@PathVariable("size") int size) throws Exception {
//获得Http客户端
CloseableHttpClient build = HttpClientBuilder.create().build();
//创建post请求
HttpPost httpPost = new HttpPost("http://localhost:8088/position/findAll/"+page+"/"+size);
//执行请求
CloseableHttpResponse execute = build.execute(httpPost);
//解析返回值
StatusLine statusLine = execute.getStatusLine();
//获取到返回状态码
System.out.println("状态码为:"+statusLine.getStatusCode());
String s = EntityUtils.toString(execute.getEntity());
build.close();
execute.close();
return s;
}
//post map传参
@RequestMapping("findById")
public String findById(@RequestParam("id") Integer id)throws Exception{
//创建httpclicent请求对象
CloseableHttpClient build = HttpClientBuilder.create().build();
//声明请求方式
HttpPost httpPost = new HttpPost("http://localhost:8088/position/findById");
//声明携带参数
Map map=new HashMap<>();
map.put("id",id);
//将map转换为json格式
Object o = JSONObject.toJSON(map);
//设置请求 参数的编码格式
StringEntity stringEntity = new StringEntity(o.toString(), "utf-8");
//将参数设置到请求对象中
httpPost.setEntity(stringEntity);
//设置content-Type
httpPost.setHeader("Content-Type","application/json");
//执行请求
CloseableHttpResponse execute = build.execute(httpPost);
//解析返回值
StatusLine statusLine = execute.getStatusLine();
//获取到返回状态码
System.out.println("状态码为:"+statusLine.getStatusCode());
String s = EntityUtils.toString(execute.getEntity());
build.close();
execute.close();
return s;
}