使用hutool发送POST请求

时间:2025-02-16 08:11:27
//创建json对象作为requestBody JSONObject jsonObject = new JSONObject(); jsonObject.put("username",user.getUsername()); jsonObject.put("password","123"); System.out.println(JSONUtil.toJsonStr(jsonObject)); // 添加请求头信息 Map<String, String > heads = new HashMap<>(); // 使用json发送请求,下面的是必须的 heads.put("Content-Type", "application/json;charset=UTF-8"); /** ** headerMap是添加的请求头, body是传入的参数,这里选择json,后端使用@RequestBody接收 */ HttpResponse response = HttpRequest.post(urlStr) .headerMap(heads, false) .body(String.valueOf(jsonObject)) .timeout(5 * 60 * 1000) .execute(); System.out.println(response);