From ca6d2dcb87d21074d9b695bb0b3a43cee86742db Mon Sep 17 00:00:00 2001 From: xupei Date: Tue, 31 Mar 2026 15:51:36 +0800 Subject: [PATCH] =?UTF-8?q?TASK:=E6=B7=BB=E5=8A=A0=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E6=97=B6http=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/da/common/utils/SendRequestUtil.java | 94 +++++++++++-------- 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/ai/da/common/utils/SendRequestUtil.java b/src/main/java/com/ai/da/common/utils/SendRequestUtil.java index aa8aec11..036d0b45 100644 --- a/src/main/java/com/ai/da/common/utils/SendRequestUtil.java +++ b/src/main/java/com/ai/da/common/utils/SendRequestUtil.java @@ -20,72 +20,92 @@ public class SendRequestUtil { @Value("${FREEPIK_API_KEY}") private String FREEPIK_API_KEY; - public String sendAliYunPostAsync(String apiUrl, String requestBody){ - // 发送POST请求 todo 异常处理 - HttpResponse execute = HttpRequest.post(apiUrl) + public String sendAliYunPostAsync(String apiUrl, String requestBody) { + // 发送POST请求 + try (HttpResponse execute = HttpRequest.post(apiUrl) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .header("X-DashScope-Async", "enable") .header(Header.CONTENT_TYPE, "application/json") .body(requestBody) .timeout(20000) // 设置超时时间20秒 - .execute(); - int status = execute.getStatus(); - if (status == 200){ - String body = execute.body(); - JSONObject bodyJson = JSONUtil.parseObj(body); - return body; + .execute()) { + + int status = execute.getStatus(); + if (status == 200) { + String body = execute.body(); + JSONObject bodyJson = JSONUtil.parseObj(body); + return body; + } + log.warn("请求失败,状态码为:{},响应内容:{}", status, execute.body()); + } catch (Exception e) { + log.error("请求阿里云API时发生异常,URL:{},请求体:{},异常信息:{}", + apiUrl, requestBody, e.getMessage(), e); } - log.warn("请求失败,状态码为 : {}", status); return null; } - public String sendAliYunPost(String apiUrl, String requestBody){ - // 发送POST请求 todo 异常处理 - HttpResponse execute = HttpRequest.post(apiUrl) + public String sendAliYunPost(String apiUrl, String requestBody) { + // 发送POST请求 + try (HttpResponse execute = HttpRequest.post(apiUrl) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .header(Header.CONTENT_TYPE, "application/json") .body(requestBody) .timeout(20000) // 设置超时时间20秒 - .execute(); - int status = execute.getStatus(); - if (status == 200){ - String body = execute.body(); - JSONObject bodyJson = JSONUtil.parseObj(body); - return body; + .execute()) { + + int status = execute.getStatus(); + if (status == 200) { + String body = execute.body(); + JSONObject bodyJson = JSONUtil.parseObj(body); + return body; + } + log.warn("请求失败,状态码为:{},响应内容:{}", status, execute.body()); + } catch (Exception e) { + log.error("请求阿里云API时发生异常,URL:{},请求体:{},异常信息:{}", + apiUrl, requestBody, e.getMessage(), e); } - log.warn("请求失败,状态码为 : {}", status); return null; } public static final String FREE_PIK = "https://api.freepik.com/v1/ai/beta/text-to-image/reimagine-flux"; - public String sendFreepikPost( String requestBody){ - // 发送POST请求 todo 异常处理 - HttpResponse execute = HttpRequest.post(FREE_PIK) + public String sendFreepikPost(String requestBody) { + // 发送POST请求 + try (HttpResponse execute = HttpRequest.post(FREE_PIK) .header(Header.CONTENT_TYPE, "application/json") .header("x-freepik-api-key", FREEPIK_API_KEY) .body(requestBody) .timeout(20000) // 设置超时时间20秒 - .execute(); - int status = execute.getStatus(); - if (status == 200){ - return execute.body(); + .execute()) { + + int status = execute.getStatus(); + if (status == 200) { + return execute.body(); + } + log.warn("请求失败,状态码为:{},响应内容:{}", status, execute.body()); + } catch (Exception e) { + log.error("请求Freepik API时发生异常,请求体:{},异常信息:{}", + requestBody, e.getMessage(), e); } - log.warn("请求失败,状态码为 : {}", status); return null; } - public String sendAliYunGet(String fullUrl){ - // 发送GET请求 todo 异常处理 - HttpResponse httpResponse = HttpRequest.get(fullUrl) + public String sendAliYunGet(String fullUrl) { + // 发送GET请求 + try (HttpResponse httpResponse = HttpRequest.get(fullUrl) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .timeout(20000) // 设置超时时间20秒 - .execute(); - int status = httpResponse.getStatus(); - if (status == 200){ - return httpResponse.body(); - }else { - return null; + .execute()) { + + int status = httpResponse.getStatus(); + if (status == 200) { + return httpResponse.body(); + } + log.warn("请求失败,状态码为:{},响应内容:{}", status, httpResponse.body()); + } catch (Exception e) { + log.error("请求阿里云API时发生异常,URL:{},异常信息:{}", + fullUrl, e.getMessage(), e); } + return null; } /*public String sendFluxPost(String url, String requestBodyStr){