TASK:添加调用第三方时http请求的异常处理

This commit is contained in:
2026-03-31 15:51:36 +08:00
parent 26d2d95acc
commit ca6d2dcb87

View File

@@ -20,72 +20,92 @@ public class SendRequestUtil {
@Value("${FREEPIK_API_KEY}") @Value("${FREEPIK_API_KEY}")
private String FREEPIK_API_KEY; private String FREEPIK_API_KEY;
public String sendAliYunPostAsync(String apiUrl, String requestBody){ public String sendAliYunPostAsync(String apiUrl, String requestBody) {
// 发送POST请求 todo 异常处理 // 发送POST请求
HttpResponse execute = HttpRequest.post(apiUrl) try (HttpResponse execute = HttpRequest.post(apiUrl)
.header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
.header("X-DashScope-Async", "enable") .header("X-DashScope-Async", "enable")
.header(Header.CONTENT_TYPE, "application/json") .header(Header.CONTENT_TYPE, "application/json")
.body(requestBody) .body(requestBody)
.timeout(20000) // 设置超时时间20秒 .timeout(20000) // 设置超时时间20秒
.execute(); .execute()) {
int status = execute.getStatus();
if (status == 200){ int status = execute.getStatus();
String body = execute.body(); if (status == 200) {
JSONObject bodyJson = JSONUtil.parseObj(body); String body = execute.body();
return 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; return null;
} }
public String sendAliYunPost(String apiUrl, String requestBody){ public String sendAliYunPost(String apiUrl, String requestBody) {
// 发送POST请求 todo 异常处理 // 发送POST请求
HttpResponse execute = HttpRequest.post(apiUrl) try (HttpResponse execute = HttpRequest.post(apiUrl)
.header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
.header(Header.CONTENT_TYPE, "application/json") .header(Header.CONTENT_TYPE, "application/json")
.body(requestBody) .body(requestBody)
.timeout(20000) // 设置超时时间20秒 .timeout(20000) // 设置超时时间20秒
.execute(); .execute()) {
int status = execute.getStatus();
if (status == 200){ int status = execute.getStatus();
String body = execute.body(); if (status == 200) {
JSONObject bodyJson = JSONUtil.parseObj(body); String body = execute.body();
return 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; return null;
} }
public static final String FREE_PIK = "https://api.freepik.com/v1/ai/beta/text-to-image/reimagine-flux"; public static final String FREE_PIK = "https://api.freepik.com/v1/ai/beta/text-to-image/reimagine-flux";
public String sendFreepikPost( String requestBody){ public String sendFreepikPost(String requestBody) {
// 发送POST请求 todo 异常处理 // 发送POST请求
HttpResponse execute = HttpRequest.post(FREE_PIK) try (HttpResponse execute = HttpRequest.post(FREE_PIK)
.header(Header.CONTENT_TYPE, "application/json") .header(Header.CONTENT_TYPE, "application/json")
.header("x-freepik-api-key", FREEPIK_API_KEY) .header("x-freepik-api-key", FREEPIK_API_KEY)
.body(requestBody) .body(requestBody)
.timeout(20000) // 设置超时时间20秒 .timeout(20000) // 设置超时时间20秒
.execute(); .execute()) {
int status = execute.getStatus();
if (status == 200){ int status = execute.getStatus();
return execute.body(); 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; return null;
} }
public String sendAliYunGet(String fullUrl){ public String sendAliYunGet(String fullUrl) {
// 发送GET请求 todo 异常处理 // 发送GET请求
HttpResponse httpResponse = HttpRequest.get(fullUrl) try (HttpResponse httpResponse = HttpRequest.get(fullUrl)
.header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY) .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
.timeout(20000) // 设置超时时间20秒 .timeout(20000) // 设置超时时间20秒
.execute(); .execute()) {
int status = httpResponse.getStatus();
if (status == 200){ int status = httpResponse.getStatus();
return httpResponse.body(); if (status == 200) {
}else { return httpResponse.body();
return null; }
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){ /*public String sendFluxPost(String url, String requestBodyStr){