使用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; // 或者根据需要处理异常
        }
    }
相关推荐
哈喽姥爷5 分钟前
Spring Boot---自动配置原理和自定义Starter
java·spring boot·后端·自定义starter·自动配置原理
啊?啊?29 分钟前
18 从对象内存到函数调用:C++ 虚函数表原理(继承覆盖 / 动态绑定)+ 多态实战
开发语言·c++·多态原理
bkspiderx35 分钟前
C++标准库:文件流类
开发语言·c++
siy23331 小时前
[c语言日记] 数组的一种死法和两种用法
c语言·开发语言·笔记·学习·链表
njxiejing1 小时前
Python NumPy安装、导入与入门
开发语言·python·numpy
Rhys..2 小时前
Python&Flask 使用 DBUtils 创建通用连接池
开发语言·python·mysql
土了个豆子的2 小时前
04.事件中心模块
开发语言·前端·visualstudio·单例模式·c#
老华带你飞2 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·vue.js·spring boot·考研·小程序·毕设·考研论坛平台小程序
CHEN5_022 小时前
leetcode-hot100 11.盛水最多容器
java·算法·leetcode
songx_992 小时前
leetcode18(无重复字符的最长子串)
java·算法·leetcode