使用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 小时前
动态创建Agent02
java
吃好睡好便好5 小时前
用while循环语句求和
开发语言·学习·算法·matlab·信息可视化
ZFSS5 小时前
Localization Translate API 集成与使用指南
java·服务器·数据库·人工智能·mysql·ai编程
TechWayfarer5 小时前
查询IP所在地的3种方案:从API到离线库,风控场景怎么选?
开发语言·网络·python·网络协议·tcp/ip
摇滚侠5 小时前
Java 零基础全套教程,集合框架,笔记 153-163
java·开发语言·笔记
nannan12325 小时前
后端技术栈梳理
java
程序员榴莲5 小时前
Python 单例模式
开发语言·python·单例模式
L、2186 小时前
CANN算子开发调试实战:从“Segmentation Fault“到定位根因的完整流程
java·开发语言
狗凯之家源码网6 小时前
基于PHP的多语言跨境电商B2B2C商城系统技术解析
开发语言·php
索木木7 小时前
NCCL SHARP 和 TREE算法
java·服务器·算法