话题不多说,直接开整
1、先导入依赖
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>common-http-starter</artifactId>
<version>0.4.2</version>
</dependency>
2、测试类开始测试
/**
* 测试GET请求
*/
@Test
public void testGet() {
Response response = HttpUtil.get("https://github.com/search", Pair.of("q", "okhttp"));
System.out.println("response = " + response);
}
/**
* 测试POST请求
*/
@Test
public void testPost() {
// JSON 格式的body
Response post = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=27f5954ab60ea8b2e431ae9101b1289c138e85aa6eb6e3940c35ee13ff8b6335", "{\"msgtype\": \"text\",\"text\": {\"content\":\"【反馈提醒】我就是我, 是不一样的烟火\"}}");
System.out.println("post = " + post);
// form 请求
Map<String, Object> formParams = new HashMap<>(16);
formParams.put("username", "qbb");
formParams.put("password", "Qiu123456.");
Response response = HttpUtil.postForm("http://127.0.0.1:8080/user/login",
formParams
);
System.out.println("response = " + response);
}
/**
* 测试JSON响应
*/
@Test
public void testJson() {
JSONObject object = HttpJsonUtil.get("http://127.0.0.1:8080/user/list/1/5",
Pair.of("q", "http"),
Pair.of("username", "qbbit"));
System.out.println("object = " + object);
}
/**
* 测试文件下载
*
* @throws IOException
*/
@Test
public void testDown() throws IOException {
HttpUtil.down("https://gitee.com/admin4j/common-http", "serverFile/");
}
标签:http,请求,void,formParams,超好,println,Test,Http,response
From: https://www.cnblogs.com/qbbit/p/16935861.html