使用 Hutool 5.8.8 版本进行 POST 请求并处理响应的代码示例

使用 Hutool 5.8.8 版本进行 POST 请求并处理响应的代码示例,其中包括发送用户实体类的 JSON 格式数据,并接收一个 Result 类,其中的 data 字段是一个用户列表

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

public class Main {
public static void main(String[] args) {
// 请求 URL
String url = “http://example.com/api/user”;

    // 构建用户实体类对象(假设为 User 类)
    User user = new User();
    user.setUsername("john");
    user.setPassword("123456");

    // 将用户实体类对象转换为 JSON 字符串
    String requestBody = JSONUtil.toJsonStr(user);

    // 发送 POST 请求
    HttpResponse response = HttpRequest.post(url)
            .body(requestBody)
            .execute();

    // 获取响应状态码
    int statusCode = response.getStatus();

    if (statusCode == 200) {
        // 请求成功,获取响应内容
        String responseBody = response.body();

        // 对响应内容进行处理
       
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值