kevinsawicki发送HTTP(S)请求

kevinsawicki是一个小巧又便捷的HTTP(S)请求利器。有时,如果只是向做快捷简易的HTTP(S)请求,那么kevinsawicki将会是一种不错的选择。

声明:本人测试时软硬件环境为:Windows10、idea、SpringBoot-2.0.5.RELEASE

准备工作:除了SpringBoot基本依赖外,还需引入kevinsawicki依赖
<!-- https://mvnrepository.com/artifact/com.github.kevinsawicki/http-request -->
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>6.0</version>
</dependency>
 

说明:本人发送请求的代码写在单元测试里,请求的URL是写在同一个项目的controller包下的。

GET请求示例:
/**
* GET请求测试
*
* @date 2018/10/31 14:37
*/
@Test
public void getRequestTest() throws UnsupportedEncodingException {
// 如果URL中含有特殊的字符值,那么需要编码一下
String symbolValue = URLEncoder.encode("&", "UTF-8");
String url = "http://127.0.0.1:8080/get?name=lisi&age=18&symbol=" + symbolValue;
HttpRequest httpRequest = HttpRequest.get(url);
// 执行请求,并返回请求体数据
String responseBody = httpRequest.body();
System.out.println(responseBody);
}
该请求对应的Controlelr层中的方法是:

/**
* 测试 -> GET参请求
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getController(@RequestParam("name") String name, Integer age, String symbol) {
String str = "age:" + name + " age:" + age + " symbol:" + symbol;
return "欢迎来到getController! -> " + str;
}
执行该请求,输出结果为:

 

POST请求(数据放在请求体中)示例:
/**
* POST请求体 --- 测试
*
* @date 2018/10/31 14:37
*/
@Test
public void postRequestTest() {
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/post/requestBody", "POST");
httpRequest.contentType("application/json", "UTF-8");
// 将请求体信息放入send中
httpRequest.send("i am data 我是json数据!");
System.out.println(httpRequest.body());
}
该请求对应的Controlelr层中的方法是:

/**
* 测试 -> POST请求体
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/post/requestBody", method = RequestMethod.POST)
public String requestBodyController(@RequestBody String str) {
return "欢迎来到requestBodyController! -> " + str;
}
执行该请求,输出结果为:

 

发送表单信息(以POST)示例:
/**
* 表单数据 --- 测试
*
* @date 2018/10/31 14:37
*/
@Test
public void formTest() {
Map<String, Object> data = new HashMap<>(8);
data.put("name", "JustryDeng");
data.put("age", 24);
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/post/formInfo", "POST");
httpRequest.form(data);
System.out.println(httpRequest.body());
}
该请求对应的Controlelr层中的方法是:

/**
* 测试 -> 表单信息
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/post/formInfo", method = RequestMethod.POST)
public String formInfoController(@RequestParam("name") String name, Integer age) {
String str = "age:" + name + " age:" + age;
return "欢迎来到formInfoController! -> " + str;
}
执行该请求,输出结果为:

 


请求头域信息设置示例:
/**
* 请求头域测试
* 注:头域里面的key,是不区分大小写的;
* 值还是会区分大小写的
*
* @date 2018/10/31 14:37
*/
@Test
public void headerTest() {
HttpRequest httpRequest = new HttpRequest("http://127.0.0.1:8080/requestHeadInfo", "GET");
// 往请求头域中添加信息的同通用方式
httpRequest.header("my-name", "邓沙利文");
httpRequest.header("my-gender", "男");
httpRequest.header("my-age", "24");
httpRequest.header("my-motto", "我是一只小小小小鸟~");
// 请求头域中经常会用到的的信息,HttpRequest又专门给我们提供了一些方法,如:
httpRequest.authorization("AuThoriZatIon");
httpRequest.contentType("text/xml", "UTF-8");
System.out.println(httpRequest.body());
}
该请求对应的Controlelr层中的方法是:

/**
* 测试 -> 请求头
* 注:头域中的key(name)不区分大小写
* value区分大小写
*
* @date 2018/10/31 14:04
*/
@RequestMapping(value = "/requestHeadInfo", method = {RequestMethod.GET, RequestMethod.POST})
public String requestHeadInfoController(HttpServletRequest request) throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder(32);
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
// 获取到请求头中的key、value
String key = preventGarbledHandler(headerNames.nextElement());
String value = preventGarbledHandler(request.getHeader(key));
sb.append("key(name)为:").append(key);
sb.append(" value为:").append(value);
sb.append(" ");
}
// 请求域中的key,是不区分大小写的;所以下面这四个都能取出来
System.out.println("第一次取值:" + request.getHeader("authorization"));
System.out.println("第二次取值:" + request.getHeader("Authorization"));
System.out.println("第三次取值:" + request.getHeader("AUTHORization"));
System.out.println("第四次取值:" + request.getHeader("authoriZAtion"));
return "欢迎来到requestHeadInfoController! -> " + sb;
}

/**
* 防乱码处理
*/
private String preventGarbledHandler(String originStr) throws UnsupportedEncodingException {
return new String(originStr.getBytes("ISO-8859-1"), "UTF-8");
}
执行该请求,输出结果为:

 

对比请求时设置的authorization的值以及该controller方法的四个输出,可以验证【头域中的key(name)不区分大小写,value区分大小写】:

 

HTTPS:
发送HTTPS请求准备工作:提供需要HTTPS访问的API。
注:因为我是用HTTPS访问自己的Controller,所以需要提供HTTPS访问的API,如果是用HTTPS访问别人,那
     么无需此准备工作。
注:如果配置了HTTPS后,如果没有再配置HTTP,那么会发现前面的HTTP请求没法访问了。本人为了快速
     测试说明,就不再详细说明如何提供HTTP/HTTPS都可访问的API了。
      可详见笔者这篇博客https://blog.csdn.net/justry_deng/article/details/82684505。

HTTPS请求示例:
/**
* HTTPS请求测试
*
* @date 2018/10/31 14:37
*/
@Test
public void httpsTest() throws UnsupportedEncodingException {
// 如果URL中含有特殊的字符值,那么需要编码一下
String name = URLEncoder.encode("邓沙利文", "UTF-8");
HttpRequest httpsRequest = HttpRequest.post("https://127.0.0.1:8080/https?name=" + name);
//Accept all certificates
httpsRequest.trustAllCerts();
//Accept all hostnames
httpsRequest.trustAllHosts();
System.out.println(httpsRequest.body());
}
该请求对应的Controlelr层中的方法是:

/**
* 测试 -> HTTPS
*
* @date 2018/10/31 21:09
*/
@RequestMapping(value = "/https", method = {RequestMethod.GET, RequestMethod.POST})
public String httpsController(@RequestParam("name") String name) {
return name + "进入HTTPS了!HTTPS调用成功!";
}
执行该请求,输出结果为:

 

注:kevinsawicki源码清晰明了,建议有时间阅读。

注:本节是对https://github.com/kevinsawicki/http-request/tree/master中列举出来的kevinsawicki各个功
     能中的部分功能的示例。更多细节、更多功能建议自行参看该链接。

 
————————————————
版权声明:本文为CSDN博主「justry_deng」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/justry_deng/article/details/83591505

原文地址:https://www.cnblogs.com/telwanggs/p/13116444.html