Http请求超好用的工具类

话题不多说,直接开整

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/");
}
posted @   我也有梦想呀  阅读(226)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示