TASK:添加调用第三方时http请求的异常处理
This commit is contained in:
@@ -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){
|
||||
|
||||
Reference in New Issue
Block a user