Hutool 发送 HTTP 请求的几种常见写法

最简单的 GET 请求:

java 复制代码
String result = HttpUtil.get("https://www.baidu.com");

带参数的 GET 请求:

java 复制代码
// 方法1: 直接拼接URL参数
String result = HttpUtil.get("https://www.baidu.com?name=张三&age=18");

// 方法2: 使用 HashMap 构建参数
HashMap<String, Object> params = new HashMap<>();
params.put("name", "张三");
params.put("age", "18");
String result = HttpUtil.get("https://www.baidu.com", params);

POST 请求:

java 复制代码
// 简单POST
String result = HttpUtil.post("https://www.baidu.com", "body content");

// 带表单参数的POST
HashMap<String, Object> params = new HashMap<>();
params.put("name", "张三");
params.put("age", "18");
String result = HttpUtil.post("https://www.baidu.com", params);

发送 JSON:

java 复制代码
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("name", "张三");
paramMap.put("age", 18);

String result = HttpRequest.post("https://www.baidu.com")
    .header("Content-Type", "application/json")
    .body(JSONUtil.toJsonStr(paramMap))
    .execute()
    .body();

高度自定义的请求:

java 复制代码
HttpRequest request = HttpRequest.post("https://www.baidu.com")
    .header("Authorization", "Bearer token123")
    .header("Custom-Header", "value")
    .timeout(20000)  //超时时间20秒
    .form(params)    //表单参数
    .cookie("sessionId", "abc123");

HttpResponse response = request.execute();
String result = response.body();
int status = response.getStatus();

文件上传:

java 复制代码
HashMap<String, Object> params = new HashMap<>();
params.put("file", FileUtil.file("path/file.jpg"));
String result = HttpUtil.post("https://www.baidu.com/upload", params);

处理响应:

java 复制代码
HttpResponse response = HttpRequest.get("https://www.baidu.com").execute();
// 获取响应状态码
int status = response.getStatus();
// 获取响应头
String contentType = response.header("Content-Type");
// 获取响应体
String body = response.body();
// 获取Cookies
List<HttpCookie> cookies = response.getCookies();

设置代理:

java 复制代码
HttpRequest.get("https://www.baidu.com")
    .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
    .execute();

请求建议:

  1. 建议在生产环境中设置合适的超时时间
  2. 对于大量请求,使用 HttpUtil.createGet() 或 HttpUtil.createPost() 预创建请求对象
  3. 处理响应时注意异常处理
  4. 如果需要复用连接,考虑使用 HttpUtil.createHttp() 创建客户端

错误处理示例:

java 复制代码
try {
    HttpResponse response = HttpRequest.get("https://www.baidu.com")
        .timeout(20000)
        .execute();
    if (response.isOk()) {
        String result = response.body();
        // 处理正常响应
    }
} catch (HttpException e) {
    // 处理超时等网络异常
    e.printStackTrace();
} catch (Exception e) {
    // 处理其他异常
    e.printStackTrace();
}
相关推荐
JhonKI29 分钟前
【Linux网络】构建HTTP响应与请求处理系统 - HttpResponse从理解到实现
linux·网络·http
猩猩—点灯36 分钟前
《TCP/IP详解 卷1:协议》之第七、八章:Ping && Traceroute
网络·tcp/ip
CHTXRT39 分钟前
2025第十六届蓝桥杯大赛(软件赛)网络安全赛 Writeup
c语言·网络·web安全·网络安全·蓝桥杯·wireshark
时迁2471 小时前
【计算机网络】TCP的四种拥塞控制算法
网络·tcp/ip·计算机网络
数据与人工智能律师1 小时前
正确应对监管部门的数据安全审查
大数据·网络·数据库·人工智能·区块链
FPGA_Linuxer2 小时前
FPGA 100G UDP纯逻辑协议栈
网络协议·fpga开发·udp
网络工程师_ling3 小时前
【WLAN】华为无线AC双机热备负载分担—双链路热备份
运维·网络
Synfuture阳途3 小时前
网络准入控制系统:2025年网络安全的坚固防线
网络·安全·web安全
科技小E4 小时前
EasyRTC音视频实时通话在线教育解决方案:打造沉浸式互动教学新体验
网络·音视频
网络工程师_ling4 小时前
【华为】防火墙双击热备-之-主备模式-单外网线路-分享
网络