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();
}
相关推荐
国科安芯7 分钟前
抗辐照MCU在卫星载荷电机控制器中的实践探索
网络·嵌入式硬件·硬件工程·智能硬件·空间计算
EasyDSS1 小时前
国标GB28181设备管理软件EasyGBS远程视频监控方案助力高效安全运营
网络·人工智能
玩转4G物联网1 小时前
零基础玩转物联网-串口转以太网模块如何快速实现与TCP服务器通信
服务器·网络·物联网·网络协议·tcp/ip·http·fs100p
派阿喵搞电子2 小时前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
光芒Shine2 小时前
【物联网-ModBus-ASCII】
物联网·网络协议
hie988942 小时前
HTTP常见的请求方法、响应状态码、接口规范介绍
http
搬码临时工3 小时前
外网访问内网服务器常用的三种简单操作步骤方法,本地搭建网址轻松让公网连接
服务器·网络·智能路由器
帽儿山的枪手3 小时前
程序员必掌握的iptables五表五链
linux·网络协议
Fortinet_CHINA3 小时前
引领AI安全新时代 Accelerate 2025北亚巡展·北京站成功举办
网络·安全
dustcell.4 小时前
Cisco Packer Tracer 综合实验
网络