|
|
@@ -1,6 +1,5 @@
|
|
|
package cn.com.goldenwater.utils;
|
|
|
|
|
|
-import cn.com.goldenwater.exception.OkHttpException;
|
|
|
import okhttp3.*;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -8,6 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -198,21 +198,67 @@ public class OkHttpUtils {
|
|
|
}
|
|
|
|
|
|
private static String httpCall(Request request) {
|
|
|
- Response response = null;
|
|
|
- Call call = null;
|
|
|
- try {
|
|
|
- call = client.newCall(request);
|
|
|
- response = call.execute();
|
|
|
- ResponseBody responseBody = response.body();
|
|
|
- return responseBody == null ? "" : responseBody.string();
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("Unexpected code: " + response.code());
|
|
|
+ }
|
|
|
+
|
|
|
+ ResponseBody body = response.body();
|
|
|
+ if (body == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 方法1:优先使用服务器声明的编码
|
|
|
+ String charset = "UTF-8";
|
|
|
+ MediaType contentType = body.contentType();
|
|
|
+ if (contentType != null && contentType.charset() != null) {
|
|
|
+ charset = contentType.charset().name();
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] responseBytes = body.bytes();
|
|
|
+ String responseString = new String(responseBytes, charset);
|
|
|
+
|
|
|
+ // 方法2:如果仍有乱码,尝试自动检测
|
|
|
+ if (hasGarbledText(responseString)) {
|
|
|
+ responseString = autoDetectEncoding(responseBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ return responseString;
|
|
|
+
|
|
|
} catch (IOException e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
- throw new OkHttpException("request error", e);
|
|
|
- } finally {
|
|
|
- Optional.ofNullable(call).ifPresent(Call::cancel);
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private static boolean hasGarbledText(String text) {
|
|
|
+ // 检查是否包含乱码特征(如大量问号或乱码符号)
|
|
|
+ return text.matches(".*[����]+.*") ||
|
|
|
+ (text.length() > 10 && text.chars().filter(ch -> ch == '?').count() > text.length() / 5);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String autoDetectEncoding(byte[] bytes) {
|
|
|
+ String[] encodings = {"UTF-8", "GBK", "GB2312", "ISO-8859-1"};
|
|
|
+
|
|
|
+ for (String encoding : encodings) {
|
|
|
+ try {
|
|
|
+ String result = new String(bytes, encoding);
|
|
|
+ if (!hasGarbledText(result) && containsValidChinese(result)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 继续尝试下一种编码
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认回退到 UTF-8
|
|
|
+ return new String(bytes, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean containsValidChinese(String str) {
|
|
|
+ return str.matches(".*[\\u4e00-\\u9fa5]+.*");
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 把request转为map
|
|
|
*
|
|
|
@@ -310,7 +356,12 @@ public class OkHttpUtils {
|
|
|
for (Map.Entry<String, Object> entry : headers.entrySet()) {
|
|
|
requestBuilder.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
|
|
|
}
|
|
|
+ } else {
|
|
|
+ headers = new HashMap<>();
|
|
|
}
|
|
|
+ // 明确要求 UTF-8 编码的响应
|
|
|
+ headers.putIfAbsent("Accept-Charset", "UTF-8");
|
|
|
+ headers.putIfAbsent("Accept", "application/json;charset=UTF-8");
|
|
|
|
|
|
// 添加Cookies
|
|
|
if (cookies != null) {
|