diff --git a/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/jdk/JdkClientEngine.java b/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/jdk/JdkClientEngine.java index f51298cebc..20a1bb3d16 100644 --- a/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/jdk/JdkClientEngine.java +++ b/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/jdk/JdkClientEngine.java @@ -119,10 +119,7 @@ public class JdkClientEngine extends AbstractClientEngine { } if (HttpStatus.isRedirected(code)) { - // https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7 - // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections - // 307方法和消息主体都不发生变化。 - if (HttpStatus.HTTP_TEMP_REDIRECT != code) { + if (HttpStatus.isRedirectToGet( code)) { // 重定向默认使用GET message.method(Method.GET); } diff --git a/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/okhttp/OkHttpEngine.java b/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/okhttp/OkHttpEngine.java index 6638b10476..fea4cf4976 100644 --- a/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/okhttp/OkHttpEngine.java +++ b/hutool-http/src/main/java/cn/hutool/v7/http/client/engine/okhttp/OkHttpEngine.java @@ -173,10 +173,7 @@ public class OkHttpEngine extends AbstractClientEngine { if (HttpStatus.isRedirected(code)) { message.locationTo(response.header(HeaderName.LOCATION.getValue())); } - // https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7 - // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections - // 307方法和消息主体都不发生变化。 - if (HttpStatus.HTTP_TEMP_REDIRECT != code) { + if (HttpStatus.isRedirectToGet( code)) { // 重定向默认使用GET message.method(Method.GET); } diff --git a/hutool-http/src/main/java/cn/hutool/v7/http/meta/HttpStatus.java b/hutool-http/src/main/java/cn/hutool/v7/http/meta/HttpStatus.java index aa5fdb6bf5..1d5ecee76d 100644 --- a/hutool-http/src/main/java/cn/hutool/v7/http/meta/HttpStatus.java +++ b/hutool-http/src/main/java/cn/hutool/v7/http/meta/HttpStatus.java @@ -375,4 +375,22 @@ public interface HttpStatus { return false; } } + + /** + * 是否为重定后请求变更为GET方法,307、308方法消息主体都不发生变化,见:
+ * + * + * @param responseCode 被检查的状态码 + * @return 是否为重定向状态码且需要使用GET方法 + * @since 7.0.0 + */ + static boolean isRedirectToGet(final int responseCode) { + return switch (responseCode) { + case HTTP_TEMP_REDIRECT, HTTP_PERMANENT_REDIRECT -> false; + default -> true; + }; + } }