java开发hutool HttpUtil网络请求工具使用demo

 更新时间:2023年07月09日 14:31:00   作者:AC编程  
这篇文章主要为大家介绍了hutool之HttpUtil网络请求工具使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、测试代码

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class Test {
    public static void main(String[] args) {
        requestToken();
    }
    public static String requestToken() {
        try {
            String url = "https://alanchen.com/auth/getToken/V2";
            HttpRequest request = HttpUtil.createPost(url);
            request.header("PartnerCode", "testCode");
            Map<String, String> param = new HashMap();
            param.put("partnerCode", "testCode");
            param.put("partnerSecret", "secret");
            Gson gson = new Gson();
            String body = gson.toJson(param);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
                throw new RuntimeException(execute.body());
            }
            String res = UnicodeUtil.toString(execute.body());
            JSONObject jsonObject = JSONUtil.parseObj(res, true);
            AuthTokenResResult resultObj = jsonObject.toBean(AuthTokenResResult.class, true);
            log.info("requestToken,resultObj={}", resultObj);
            if (resultObj.getCode() != 200) {
                log.error("获取token失败,code={},msg={},result={}", resultObj.getCode(), resultObj.getMsg(), resultObj);
                throw new RuntimeException("获取token失败,code=" + resultObj.getCode() + ",msg=" + resultObj.getMsg());
            }
            if (resultObj.getData() == null) {
                log.error("获取token为空,code={},msg={},result={}", resultObj.getCode(), resultObj.getMsg(), resultObj);
                throw new RuntimeException("获取token为空,code=" + resultObj.getCode() + ",msg=" + resultObj.getMsg());
            }
            return resultObj.getData().getToken();
        } catch (Exception e) {
            log.error("requestToken失败,msg={}", e.getMessage());
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    @Data
    public class AuthTokenResResult {
        private int code;
        private String msg;
        private AuthToken data;
        private long count;
    }
    @Data
    public class AuthToken {
        private String token;
        private long expireAt;
    }
}

二、代码片段

 // 设置请求体参数
String requestBody = "{\"param1\": \"value1\", \"param2\": \"value2\"}";
httpRequest.body(requestBody)
        .setHeader("Content-Type", "application/json")
        .timeout(20000); // 设置超时时间为20秒
// 设置请求参数
httpRequest.setQueryParam("param1", "value1")
          .setHeader("User-Agent", "Hutool")
          .timeout(20000); // 设置超时时间为20秒

以上就是java开发hutool HttpUtil网络请求工具使用demo的详细内容,更多关于hutool HttpUtil网络请求的资料请关注脚本之家其它相关文章!

相关文章

  • 一文带你搞懂SpringBoot中自动装配原理

    一文带你搞懂SpringBoot中自动装配原理

    这篇文章主要为大家详细介绍了SpringBoot中自动装配原理的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2025-01-01
  • springboot 项目使用jasypt加密数据源的方法

    springboot 项目使用jasypt加密数据源的方法

    Jasypt 是一个 Java 库,它允许开发者以最小的努力为他/她的项目添加基本的加密功能,而且不需要对密码学的工作原理有深刻的了解。接下来通过本文给大家介绍springboot 项目使用jasypt加密数据源的问题,一起看看吧
    2021-11-11
  • Spring Boot 的注解生效详细步骤解析

    Spring Boot 的注解生效详细步骤解析

    Spring通过扫描、解析、注册分层处理配置类注解(如@Configuration、@ComponentScan等),利用递归、延迟加载和缓存机制,实现BeanDefinition的动态注册与灵活扩展,解决依赖和性能问题,本文给大家介绍Spring Boot的注解生效详细步骤解析,感兴趣的朋友跟随小编一起看看吧
    2025-09-09
  • Java中的单向链表详解

    Java中的单向链表详解

    这篇文章主要介绍了Java中的单向链表详解,单向链表又叫单链表,是链表的一种,由节点构成,head指针指向第一个称为表头节点,而终止指向最后一个null指针,需要的朋友可以参考下
    2024-01-01
  • Hibernate处理多对多关系的实现示例

    Hibernate处理多对多关系的实现示例

    本文介绍了Hibernate中实现多对多关系的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-01-01
  • SpringBoot集成使用Redis及搭建过程

    SpringBoot集成使用Redis及搭建过程

    jackson-json 工具提供了 javabean 与 json 之 间的转换能力,可以将 pojo 实例序列化成 json 格式存储在 redis 中,也可以将 json 格式的数据转换成 pojo 实例,本文给大家介绍SpringBoot集成使用Redis及搭建过程,感兴趣的朋友一起看看吧
    2022-01-01
  • Java使用itextpdf实现表单导出为pdf

    Java使用itextpdf实现表单导出为pdf

    这篇文章主要为大家详细介绍了Java如何使用itextpdf实现form表单导出为pdf,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-06-06
  • SpringBoot3.0+SpringSecurity6.0+JWT的实现

    SpringBoot3.0+SpringSecurity6.0+JWT的实现

    本文主要介绍了SpringBoot3.0+SpringSecurity6.0+JWT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • Mybatis获取参数值和查询功能的案例详解

    Mybatis获取参数值和查询功能的案例详解

    这篇文章主要介绍了Mybatis获取参数值和查询功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Spring中@Autowired自动注入map详解

    Spring中@Autowired自动注入map详解

    这篇文章主要介绍了Spring中@Autowired自动注入map详解,  spring是支持基于接口实现类的直接注入的,支持注入map,list等集合中,不用做其他的配置,直接注入,需要的朋友可以参考下
    2023-10-10

最新评论