使用hutool工具发送request 请求模板

使用hutool工具发送request 请求模板

复制代码
 private JSONArray fetchData(){
 		String url = "https://www.baidu.com";
	   	String token = "asdfasdfsdf";
        JSONObject requestBody = new JSONObject();
        JSONObject filterOption = new JSONObject();
        JSONObject filters = new JSONObject();
        filterOption.set("filters", filters);
        requestBody.set("filterOption", filterOption);
        requestBody.set("a", 1);
        requestBody.set("b", 1000);
        requestBody.set("c", "list");
        log.info(requestBody.toString());
        try (HttpResponse response = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .header("token", token)
                .body(requestBody.toString())
                .timeout(20000)  // 设置超时时间
                .execute()) {

            // 处理响应
            if (response.getStatus() == 200) {
                String responseBody = response.body();
                logger.info(responseBody);
                return new JSONObject(responseBody).getJSONArray("results");
            } else {
                logger.error("错误获取数据: " + response.getStatus() + " - " + response.body());
                throw new RuntimeException("错误获取数据");
            }

        } catch (Exception e) {
            logger.error("从收样系统获取医院数据错误", e);
            throw e; // 或者根据需要处理异常
        }
    }
相关推荐
沐知全栈开发1 小时前
C# 委托(Delegate)
开发语言
mit6.8241 小时前
[C# starter-kit] 命令/查询职责分离CQRS | MediatR |
java·数据库·c#
诸神缄默不语1 小时前
Maven用户设置文件(settings.xml)配置指南
xml·java·maven
任子菲阳1 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
学Linux的语莫2 小时前
机器学习数据处理
java·算法·机器学习
找不到、了2 小时前
JVM的即时编译JIT的介绍
java·jvm
csbysj20202 小时前
如何使用 XML Schema
开发语言
R6bandito_2 小时前
STM32中printf的重定向详解
开发语言·经验分享·stm32·单片机·嵌入式硬件·mcu
earthzhang20212 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
西瓜er2 小时前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg