Spring使用hutool的HttpRequest发送请求的几种方式
更新时间:2024年11月22日 09:23:17 作者:Yeast_东
Spring HttpRequest是指Spring框架中的一个对象,它代表了HTTP客户端发送给Web服务器的一次请求,本文给大家介绍了Spring使用hutool的HttpRequest发送请求的几种方式,并通过代码示例讲解的非常详细,需要的朋友可以参考下
hutool为我们封装了发送请求的工具,我们一起来看看常用的有哪些吧!
1.添加依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version> <!-- 请使用最新版本 -->
</dependency>2.发送get请求
2.1 直接url传参
import cn.hutool.http.HttpUtil;
import cn.hutool.core.util.StrUtil;
public class HttpGetExample {
public static void main(String[] args) {
// 定义基础 URL 和路径
String baseUrl = "http://example.com";
String path = "/api/test";
// 定义参数
String name = "zhangsan";
int age = 21;
// 构建完整的 URL
String url = StrUtil.format("{}/{}?name={}&age={}", baseUrl, path, name, age);
// 发送 GET 请求
String result = HttpUtil.get(url);
// 输出响应结果
System.out.println("Response: " + result);
}
}2.2 Map传参
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
import java.util.Map;
public class HttpGetExample {
public static void main(String[] args) {
// 定义基础 URL 和路径
String baseUrl = "http://example.com";
String path = "/api/test";
// 构建完整的 URL
String url = baseUrl + path;
// 定义参数
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 发送 GET 请求
String result = HttpUtil.get(url, params);
// 输出响应结果
System.out.println("Response: " + result);
}
}2.3 Form传参
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
import java.util.Map;
public class HttpGetExample {
public static void main(String[] args) {
// 定义基础 URL 和路径
String baseUrl = "http://example.com";
String path = "/api/test";
// 构建完整的 URL
String url = baseUrl + path;
// 定义参数
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 发送 GET 请求
String result = HttpRequest.get(url)
.form(params)
.execute()
.body();
// 输出响应结果
System.out.println("Response: " + result);
}
}3. 发送Post请求
3.1 Json传参
public static void main(String[] args) {
// 定义基础 URL 和路径
String baseUrl = "http://example.com";
String path = "/api/test";
// 构建完整的 URL
String url = baseUrl + path;
// 定义参数
String jsonString = "{\"token\":\"1234567890\",\"userId\":\"user123\",\"userName\":\"张三\"}";
// 发送 GET 请求
String result = HttpRequest.post(url)
.header("Access-Token", token) // 如果需要
.header("Content-Type","application/json")
.body(jsonString)
.execute()
.body();
// 输出响应结果
System.out.println("Response: " + result);
}3.2 Form传参
public static void main(String[] args) {
// 定义基础 URL 和路径
String baseUrl = "http://example.com";
String path = "/api/test";
// 构建完整的 URL
String url = baseUrl + path;
// 定义参数
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 发送 GET 请求
String result = HttpRequest.post(url)
.header("Content-Type","multipart/form-data;charset=UTF-8")
.form(params)
.execute()
.body();
// 输出响应结果
System.out.println("Response: " + result);
}到此这篇关于Spring使用hutool的HttpRequest发送请求的几种方式的文章就介绍到这了,更多相关Spring HttpRequest发送请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
基于RecyclerChart的KLine绘制Volume实现详解
这篇文章主要为大家介绍了基于RecyclerChart的KLine绘制Volume实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-03-03
Java Stream 的 collect 与 reduce 
在 Java Stream API 中,collect 和 reduce 是两种强大的终止操作,用于将流中的元素累积为最终结果,本文将从核心概念、使用场景、性能特性等多个维度进行对比分析,感兴趣的朋友跟随小编一起看看吧2025-09-09


最新评论