From b7ad1e82d1a75e16d6bf69166da074ed7f00403a Mon Sep 17 00:00:00 2001 From: Looly <loolly@aliyun.com> Date: Fri, 6 Sep 2024 17:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=8C=89=E7=85=A7RFC7231=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E9=99=A4=E4=BA=86307=E5=A4=96=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E4=BD=BF=E7=94=A8GET=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- .../main/java/cn/hutool/http/HttpConfig.java | 17 ----------------- .../main/java/cn/hutool/http/HttpRequest.java | 9 ++++++--- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e48f61d..fa1579710 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,11 @@ * 【core 】 用ArrayList重新实现权重随机类:WeightListRandom(pr#3720@Github) * 【crypto 】 SM2解密时,兼容GmSSL非压缩省略的04头的密文(issue#IAP1QJ@Gitee) * 【core 】 兼容NumberUtil.add方法传入整型自动类型转换为浮点类型的精度丢失问题(pr#3721@Github) -* 【http 】 HttpConfig增加配置setUseGetIfRedirect(issue#3722@Github) ### 🐞Bug修复 * 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github) * 【core 】 修复RegexPool.CHINESE_NAME范围太大的问题(issue#IAOGDR@Gitee) +* 【http 】 修复重定向没有按照RFC7231规范跳转的问题,修改为除了307外重定向使用GET方式(issue#3722@Github) ------------------------------------------------------------------------------------------------------------- **# 5.8.32(2024-08-30) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpConfig.java index 4ed97c2a2..172102e45 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpConfig.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpConfig.java @@ -100,11 +100,6 @@ public class HttpConfig { * @since 5.8.33 */ boolean useDefaultContentTypeIfNull = true; - /** - * 当重定向时,是否使用Get方式发送请求<br> - * issue#3722 部分请求要求重定向时,必须使用Get方式发送请求 - */ - boolean useGetIfRedirect; /** * 设置超时,单位:毫秒<br> @@ -337,16 +332,4 @@ public class HttpConfig { this.useDefaultContentTypeIfNull = useDefaultContentTypeIfNull; return this; } - - /** - * 重定向时是否使用GET方式 - * - * @param useGetIfRedirect 重定向时是否使用GET方式 - * @return this - * @since 5.8.33 - */ - public HttpConfig setUseGetIfRedirect(boolean useGetIfRedirect) { - this.useGetIfRedirect = useGetIfRedirect; - return this; - } } diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index 901c2cd92..b9e573630 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -1313,9 +1313,12 @@ public class HttpRequest extends HttpBase<HttpRequest> { redirectUrl = UrlBuilder.ofHttpWithoutEncode(location); } setUrl(redirectUrl); - if(config.useGetIfRedirect){ - // since 5.8.33, issue#3722 - setMethod(Method.GET); + // 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 != responseCode) { + // 重定向默认使用GET + method(Method.GET); } if (redirectCount < config.maxRedirectCount) { redirectCount++;