Browse Source

fix(http): 优化HTTP请求异常处理机制

- 移除超时异常中的线程中断操作,避免影响主线程状态
- 分离IOException和InterruptedException的处理逻辑
- 为不同异常类型提供更精确的错误信息
- 恢复被中断线程的状态标记
- 移除过时的代码注释块
master
常熟吴彦祖 1 month ago
parent
commit
46dbb32064
  1. 18
      src/main/java/com/gaotao/common/utils/HttpUtils.java

18
src/main/java/com/gaotao/common/utils/HttpUtils.java

@ -99,13 +99,19 @@ public class HttpUtils {
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
return handleResponse(response); return handleResponse(response);
} catch (java.net.http.HttpTimeoutException e) { } catch (java.net.http.HttpTimeoutException e) {
// 超时异常特殊处理 - rqrq
Thread.currentThread().interrupt();
throw new RuntimeException("接口响应超时(60秒),请检查网络连接或者接口服务是否可用。 " ); throw new RuntimeException("接口响应超时(60秒),请检查网络连接或者接口服务是否可用。 " );
} catch (IOException | InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("POST 请求失败: " + e.getMessage(), e);
}
} catch (IOException e) {
// IO 错误不涉及中断
throw new RuntimeException("网络或IO错误: " + e.getMessage(), e);
} catch (InterruptedException e) {
// 线程被中断恢复中断状态并抛出异常或返回
Thread.currentThread().interrupt(); // 合理
throw new RuntimeException("请求被中断", e);
}
// (IOException | InterruptedException e) {
// Thread.currentThread().interrupt();
// throw new RuntimeException("POST 请求失败: " + e.getMessage(), e);
// }
} }
/** 构建查询参数字符串 */ /** 构建查询参数字符串 */

Loading…
Cancel
Save