使用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; // 或者根据需要处理异常
        }
    }
相关推荐
小糯米6015 分钟前
C++顺序表和vector
开发语言·c++·算法
froginwe1110 分钟前
JavaScript 函数调用
开发语言
阔皮大师14 分钟前
INote轻量文本编辑器
java·javascript·python·c#
独望漫天星辰15 分钟前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++
小法师爱分享18 分钟前
StickyNotes,简单便签超实用
java·python
qq_2975746719 分钟前
Linux 服务器 Java 开发环境搭建保姆级教程
java·linux·服务器
金牌归来发现妻女流落街头31 分钟前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
毅炼31 分钟前
Java 基础常见问题总结(4)
java·后端
无小道33 分钟前
Qt——事件简单介绍
开发语言·前端·qt
devmoon38 分钟前
在 Paseo 测试网上获取 Coretime:On-demand 与 Bulk 的完整实操指南
开发语言·web3·区块链·测试用例·智能合约·solidity