Hutool常用四种发起请求的方法

时间:2025-02-16 08:12:45

Hutool常用四种发起请求的方法

  • 导入jar包,可以直接去搜索hutool官方查看最新版本和使用文档。
<!-- /artifact//hutool-all -->
<dependency>
    <groupId></groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.8</version>
</dependency>
POST
  • POST请求 requestUrl是请求路径,paramMap是Map参数,参数还可以是字符串body。
String resultJson1 = HttpUtil.post(requestUrl, paramMap);
JSONObject jsonObject1 = JSONUtil.parseObj(resultJson);
GET
  • GET请求 也可带Map集合参数。
String resultJson2 = HttpUtil.get(requestUrl);
JSONObject jsonObject2 = JSONUtil.parseObj(resultJson);
DELETE
  • DELETE请求,requestUrl请求路径,paramMap请求参数
  • token鉴权。
String resultJson = HttpRequest.delete(requestUrl)
    .header("Content-Type", "application/json")
    .cookie(token)
    .execute()
    .body();
JSONObject jsonObject3 = JSONUtil.parseObj(resultJson);
PUT
  • PUT请求,requestUrl请求路径,paramMap请求参数
String resultJson4 =  HttpRequest.put(requestUrl).form(paramMap).execute().body()
JSONObject jsonObject4 = JSONUtil.parseObj(resultJson);