修复HttpRequest.sendRedirectIfPossible未对308做判断问题。(issue#4053@Github)

This commit is contained in:
Looly
2025-09-03 08:57:08 +08:00
parent a4ed76242e
commit 4c61c23497
3 changed files with 20 additions and 8 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -375,4 +375,22 @@ public interface HttpStatus {
return false;
}
}
/**
* 是否为重定后请求变更为GET方法307、308方法消息主体都不发生变化<br>
* <ul>
* <li>https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7</li>
* <li>https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections</li>
* </ul>
*
* @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;
};
}
}