使用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; // 或者根据需要处理异常
        }
    }
相关推荐
Lisonseekpan13 分钟前
雪花算法(Snowflake)技术详解与实战应用
java·分布式·后端·算法
带土114 分钟前
1. Qt-的安装和环境变量的配置
开发语言·qt
披着羊皮不是狼35 分钟前
多用户跨学科交流系统(5):点赞功能的后端完整处理链路
java·spring boot·后端
q***133436 分钟前
SpringMVC新版本踩坑[已解决]
java
q***d1731 小时前
Rust并发模型
开发语言·后端·rust
Charles_go1 小时前
C#中级48、Debug版本和Release版本有什么区别
java·linux·c#
CircleMouse1 小时前
介绍几个axios接口请求顺序的问题
开发语言·前端·javascript·ecmascript
baivfhpwxf20231 小时前
用TCP服务端向多个客户端分发图片,客户端接收并处理图片,再将处理结果返回给服务端
服务器·开发语言·网络·tcp/ip·c#
资深web全栈开发1 小时前
Golang Cobra 教程:构建强大的CLI应用
开发语言·后端·golang
u***27611 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端