|
|
@@ -25,21 +25,26 @@ public class HttpUtil {
|
|
|
/**
|
|
|
* 同步 GET 请求
|
|
|
*/
|
|
|
- public static String get(String url, Map<String, String> headers) throws Exception {
|
|
|
+ public static String get(String url, Map<String, String> headers) {
|
|
|
Request.Builder builder = new Request.Builder().url(url);
|
|
|
addHeaders(builder, headers);
|
|
|
|
|
|
Request request = builder.build();
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
- if (response.isSuccessful() && response.body() != null) {
|
|
|
- String result = response.body().string();
|
|
|
- log.info("同步 GET 响应结果:{}", result);
|
|
|
- return result;
|
|
|
- } else if (!response.isSuccessful()) {
|
|
|
- throw new BusinessException("同步 GET 请求请求异常: " + response.code());
|
|
|
- } else {
|
|
|
- return "";
|
|
|
+ try {
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (response.isSuccessful() && response.body() != null) {
|
|
|
+ String result = response.body().string();
|
|
|
+ log.info("同步 GET 响应结果:{}", result);
|
|
|
+ return result;
|
|
|
+ } else if (!response.isSuccessful()) {
|
|
|
+ throw new BusinessException("同步 GET 请求请求异常: " + response.code());
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("同步 GET 响应异常: {}", e.getMessage());
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -51,13 +56,17 @@ public class HttpUtil {
|
|
|
addHeaders(builder, headers);
|
|
|
|
|
|
Request request = builder.build();
|
|
|
- client.newCall(request).enqueue(callback);
|
|
|
+ try {
|
|
|
+ client.newCall(request).enqueue(callback);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException("异步 GET 请求 异常",e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 同步 POST - 表单请求
|
|
|
*/
|
|
|
- public static String postForm(String url, Map<String, String> formData, Map<String, String> headers) throws IOException {
|
|
|
+ public static String postForm(String url, Map<String, String> formData, Map<String, String> headers) {
|
|
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
if (formData != null) {
|
|
|
for (Map.Entry<String, String> entry : formData.entrySet()) {
|
|
|
@@ -70,38 +79,49 @@ public class HttpUtil {
|
|
|
addHeaders(builder, headers);
|
|
|
|
|
|
Request request = builder.build();
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
- if (response.isSuccessful() && response.body() != null) {
|
|
|
- String result = response.body().string();
|
|
|
- log.info("同步 POST - 表单响应结果:{}", result);
|
|
|
- return result;
|
|
|
- } else if (!response.isSuccessful()) {
|
|
|
- throw new BusinessException("同步 POST - 表单请求异常: " + response.code());
|
|
|
- } else {
|
|
|
- return "";
|
|
|
+ try {
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (response.isSuccessful() && response.body() != null) {
|
|
|
+ String result = response.body().string();
|
|
|
+ log.info("同步 POST - 表单响应结果:{}", result);
|
|
|
+ return result;
|
|
|
+ } else if (!response.isSuccessful()) {
|
|
|
+ throw new BusinessException("同步 POST - 表单请求异常: " + response.code());
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步 POST - 表单异常: {}", e.getMessage());
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 同步 POST - JSON 请求
|
|
|
*/
|
|
|
- public static String postJson(String url, String json, Map<String, String> headers) throws IOException {
|
|
|
+ public static String postJson(String url, String json, Map<String, String> headers) {
|
|
|
RequestBody body = RequestBody.create(json, JSON);
|
|
|
Request.Builder builder = new Request.Builder().url(url).post(body);
|
|
|
addHeaders(builder, headers);
|
|
|
|
|
|
Request request = builder.build();
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
- if (response.isSuccessful() && response.body() != null) {
|
|
|
- String result = response.body().string();
|
|
|
- log.info("同步 POST - JSON响应结果:{}",result );
|
|
|
- return result;
|
|
|
- } else if (!response.isSuccessful()) {
|
|
|
- throw new BusinessException("同步 POST - JSON 请求异常: " + response.code());
|
|
|
- } else {
|
|
|
- return "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (response.isSuccessful() && response.body() != null) {
|
|
|
+ String result = response.body().string();
|
|
|
+ log.info("同步 POST - JSON响应结果:{}",result );
|
|
|
+ return result;
|
|
|
+ } else if (!response.isSuccessful()) {
|
|
|
+ throw new BusinessException("同步 POST - JSON 请求异常: " + response.code());
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步 POST - JSON 异常: {}", e.getMessage());
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -114,7 +134,11 @@ public class HttpUtil {
|
|
|
addHeaders(builder, headers);
|
|
|
|
|
|
Request request = builder.build();
|
|
|
- client.newCall(request).enqueue(callback);
|
|
|
+ try {
|
|
|
+ client.newCall(request).enqueue(callback);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException("异步 POST - JSON异常:{}", e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|