陈昶聿
3 天以前 d7e3192804fca756d881af360ffd5eea157ef9ad
ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java
@@ -95,6 +95,50 @@
        return httpClient;
    }
    /**
     * POST JSON,只校验 HTTP 状态码,允许响应体为空。
     * <p>
     * 用于 /hangup 这类"成功只回 200、没有响应体"的接口:
     * {@link #postJsonRequest(String, String)} 在读响应体时如果为空会抛 RESPONSE_NULL_ERROR_CODE(999994),
     * 会把一次成功的挂断误判成失败(还会触发无意义的重试)。
     * <p>
     * 非 2xx 时抛出的 HttpRequestException 里带上状态码和响应体,方便排查。
     *
     * @param url     请求地址
     * @param request json 请求体
     */
    public static void postJsonRequestAllowEmptyResponse(String url, String request) throws HttpRequestException {
        Assert.hasLength(url, "请求url不能为空字符串。");
        EntityEnclosingMethod httpMethod = new PostMethod(url);
        httpMethod.addRequestHeader("Content-Type", "application/json; charset=UTF-8");
        setHeaderRequestId(httpMethod);
        try {
            RequestEntity entity = new StringRequestEntity(request, "application/json", "utf-8");
            httpMethod.setRequestEntity(entity);
            int resultCode = httpClient.executeMethod(httpMethod);
            if (resultCode >= 200 && resultCode < 300) {
                return;
            }
            String respJson = "";
            try {
                respJson = httpMethod.getResponseBodyAsString();
            } catch (Exception ignore) {
                // 读不到响应体不影响判断,状态码已经能说明问题
            }
            throw new HttpRequestException(resultCode, respJson);
        } catch (UnsupportedEncodingException e) {
            throw new HttpRequestException(ENCODING_ERROR_CODE, e);
        } catch (HttpException e) {
            throw new HttpRequestException(HTTP_ERROR_CODE, e);
        } catch (IOException e) {
            throw new HttpRequestException(IO_ERROR_CODE, e);
        } finally {
            if (httpMethod != null) {
                httpMethod.releaseConnection();
            }
        }
    }
    public static Boolean postJsonRequest(String url, String request) throws HttpRequestException {
        Assert.hasLength(url, "请求url不能为空字符串。");
        EntityEnclosingMethod httpMethod = new PostMethod(url);