一、背景
项目中包含了服务层和执行层。服务层负责对外提供接口,执行层负责任务的调度。上述的两层抽出了两个model,即下面的图。目的是执行层使用java封装的httpClient包完成服务层的接口调用。
二、HttpClient的简单实用
2.1导入pom依赖
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</version>
</dependency>
2.2HttpClient的简单实用之get请求
服务层接口
package com.wzt.controller;
import org.springframework.web.bind.annotation.*;
/**
* @author jason
*/
@RestController
@RequestMapping("/task")
public class TaskController {
@GetMapping("/test")
public String getTest() {
return "Get请求成功";
}
}
执行层函数
/**
* 使用get方式对服务器发送请求
*/
public static void getTest() throws IOException, ParseException {
String testTasUrl = "http://localhost:8081/task/test";
CloseableHttpClient client = HttpClients.createDefault();
//发送http请求
HttpGet get = new HttpGet(testTasUrl);
CloseableHttpResponse response = client.execute(get);
//获取响应数据
System.out.println("【获取响应数据】" + EntityUtils.toString(response.getEntity()));
Header[] headers = response.getHeaders();
for (Header header : headers) {
System.out.println("\t|-【头信息】name:" + header.getName() + "、value:" + header.getValue());
}
System.out.println("【状态码为】" + response.getCode());
}
运行结果
2.3HttpClient的简单实用之get带参数请求
服务层接口
@GetMapping("/testParm")
public String getParmTest(@RequestParam String msg){
return "msg:"+msg;
}
执行层函数
/**
* 带参数的get请求
* @throws URISyntaxException
* @throws IOException
* @throws ParseException
*/
public static void getParmTest() throws URISyntaxException, IOException, ParseException {
//1.创建客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建一个封装的URI的对象,在对象中可以给定制定的参数
URIBuilder builder = new URIBuilder("http://localhost:8081/task/testParm?");
builder.addParameter("msg", "你好");
//3.创建get请求
HttpGet get = new HttpGet(builder.build());
//4.获取响应
CloseableHttpResponse response = client.execute(get);
//5.获取响应数据
System.out.println("【响应数据为】" + EntityUtils.toString(response.getEntity(),"utf-8"));
}
运行结果
2.4HttpClient的简单实用之post请求
服务层接口
@PostMapping("/test")
public String postTest() {
return "Post请求成功";
}
执行层函数
/**
* 使用post完成请求
*/
public static void postTest() throws IOException, ParseException {
//1.创建一个客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建post请求
HttpPost post = new HttpPost("http://localhost:8081/task/test");
//3.获取响应
CloseableHttpResponse response = client.execute(post);
//4.获取响应数据
System.out.println("【获取响应数据】:" + EntityUtils.toString(response.getEntity()));
//5.获取响应的状态
System.out.println("【状态码为】" + response.getCode());
}
运行结果
2.5HttpClient的简单实用之post带参数请求
服务层接口
@PostMapping("/testParm")
public String postParmTest(@RequestParam String msg){
return "post->msg:"+msg;
}
执行层接口
/**
* 使用带参数请求的post请求
*/
public static void postParmTest() throws IOException, ParseException {
//1.创建客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建带参数的请求
HttpPost post = new HttpPost("http://localhost:8081/task/testParm?");
//3.对请求的参数完成集合的配置
List<NameValuePair> allParams = new ArrayList<>();//通过集合保存传递的参数
allParams.add(new BasicNameValuePair("msg", "你好"));
//4.如果发送的post请求,需要对请求的参数进行编码配置
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(allParams, Charset.forName("UTF-8"));
//5.将请求的参数配置到Post请求中
post.setEntity(urlEncodedFormEntity);
//6.发送Http post请求
CloseableHttpResponse response = client.execute(post);
//7.获取响应的数据
System.out.println("【响应数据为】:" + EntityUtils.toString(response.getEntity()));
//8.获取响应的代码
System.out.println("状态码为:" + response.getCode());
}
运行结果
三、整体代码
服务层接口代码
package com.wzt.controller;
import org.springframework.web.bind.annotation.*;
/**
* @author jason
*/
@RestController
@RequestMapping("/task")
public class TaskController {
@GetMapping("/test")
public String getTest() {
return "Get请求成功";
}
@GetMapping("/testParm")
public String getParmTest(@RequestParam String msg) {
return "msg:" + msg;
}
@PostMapping("/test")
public String postTest() {
return "Post请求成功";
}
@PostMapping("/testParm")
public String postParmTest(@RequestParam String msg){
return "post->msg:"+msg;
}
}
执行层函数代码
package com.jason;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.net.URIBuilder;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* 使用httpclient调用当前服务端的接口
*/
public class TestHttpClient {
public static void main(String[] args) throws IOException, ParseException, URISyntaxException {
postParmTest();
}
/**
* 使用带参数请求的post请求
*/
public static void postParmTest() throws IOException, ParseException {
//1.创建客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建带参数的请求
HttpPost post = new HttpPost("http://localhost:8081/task/testParm?");
//3.对请求的参数完成集合的配置
List<NameValuePair> allParams = new ArrayList<>();//通过集合保存传递的参数
allParams.add(new BasicNameValuePair("msg", "你好"));
//4.如果发送的post请求,需要对请求的参数进行编码配置
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(allParams, Charset.forName("UTF-8"));
//5.将请求的参数配置到Post请求中
post.setEntity(urlEncodedFormEntity);
//6.发送Http post请求
CloseableHttpResponse response = client.execute(post);
//7.获取响应的数据
System.out.println("【响应数据为】:" + EntityUtils.toString(response.getEntity()));
//8.获取响应的代码
System.out.println("状态码为:" + response.getCode());
}
/**
* 使用post完成请求
*/
public static void postTest() throws IOException, ParseException {
//1.创建一个客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建post请求
HttpPost post = new HttpPost("http://localhost:8081/task/test");
//3.获取响应体
CloseableHttpResponse response = client.execute(post);
//4.解析响应题
System.out.println("【获取响应数据】:" + EntityUtils.toString(response.getEntity()));
//5.获取响应的状态
System.out.println("【状态码为】" + response.getCode());
}
/**
* 使用get方式对服务器发送请求
*/
public static void getTest() throws IOException, ParseException {
String testTasUrl = "http://localhost:8081/task/test";
CloseableHttpClient client = HttpClients.createDefault();
//发送http请求
HttpGet get = new HttpGet(testTasUrl);
CloseableHttpResponse response = client.execute(get);
//获取响应数据
System.out.println("【获取响应数据】" + EntityUtils.toString(response.getEntity()));
Header[] headers = response.getHeaders();
for (Header header : headers) {
System.out.println("\t|-【头信息】name:" + header.getName() + "、value:" + header.getValue());
}
System.out.println("【状态码为】" + response.getCode());
}
/**
* 带参数的get请求
* @throws URISyntaxException
* @throws IOException
* @throws ParseException
*/
public static void getParmTest() throws URISyntaxException, IOException, ParseException {
//1.创建客户端
CloseableHttpClient client = HttpClients.createDefault();
//2.创建一个封装的URI的对象,在对象中可以给定制定的参数
URIBuilder builder = new URIBuilder("http://localhost:8081/task/testParm?");
builder.addParameter("msg", "你好");
//3.创建get请求
HttpGet get = new HttpGet(builder.build());
//4.获取响应
CloseableHttpResponse response = client.execute(get);
//5.获取响应数据
System.out.println("【响应数据为】" + EntityUtils.toString(response.getEntity(),"utf-8"));
}
}