新增followRedirectsCookie配置,支持开启自动重定向携带cookie

This commit is contained in:
Looly 2023-03-09 00:03:40 +08:00
parent afc00aa0b0
commit 5af423ddc2
4 changed files with 16 additions and 11 deletions

View File

@ -2,12 +2,14 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.15.M1 (2023-03-08) # 5.8.15.M1 (2023-03-09)
### 🐣新特性 ### 🐣新特性
* 【http 】 新增followRedirectsCookie配置支持开启自动重定向携带cookiepr#2961@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【all 】 修复Automatic-Module-Name错误问题issue#2952@Github * 【all 】 修复Automatic-Module-Name错误问题issue#2952@Github
* 【all 】 修复NumberWithFormat导致转换Long异常问题issue#I6L2LO@Gitee * 【core 】 修复NumberWithFormat导致转换Long异常问题issue#I6L2LO@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -305,8 +305,10 @@ public class HttpConfig {
/** /**
* 自动重定向时是否处理cookie * 自动重定向时是否处理cookie
*
* @param followRedirectsCookie 自动重定向时是否处理cookie * @param followRedirectsCookie 自动重定向时是否处理cookie
* @return this * @return this
* @since 5.8.15
*/ */
public HttpConfig setFollowRedirectsCookie(boolean followRedirectsCookie) { public HttpConfig setFollowRedirectsCookie(boolean followRedirectsCookie) {
this.followRedirectsCookie = followRedirectsCookie; this.followRedirectsCookie = followRedirectsCookie;

View File

@ -1262,7 +1262,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
private HttpResponse sendRedirectIfPossible(boolean isAsync) { private HttpResponse sendRedirectIfPossible(boolean isAsync) {
// 手动实现重定向 // 手动实现重定向
if (config.maxRedirectCount > 0) { if (config.maxRedirectCount > 0) {
int responseCode; final int responseCode;
try { try {
responseCode = httpConnection.responseCode(); responseCode = httpConnection.responseCode();
} catch (IOException e) { } catch (IOException e) {
@ -1271,6 +1271,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
throw new HttpException(e); throw new HttpException(e);
} }
// 支持自动重定向时处理cookie // 支持自动重定向时处理cookie
// https://github.com/dromara/hutool/issues/2960
if (config.followRedirectsCookie) { if (config.followRedirectsCookie) {
GlobalCookieManager.store(httpConnection); GlobalCookieManager.store(httpConnection);
} }

View File

@ -20,7 +20,7 @@ public class RedirectServerTest {
response.addHeader(Header.SET_COOKIE.getValue(), "redirect3=3; path=/; HttpOnly"); response.addHeader(Header.SET_COOKIE.getValue(), "redirect3=3; path=/; HttpOnly");
response.send(301); response.send(301);
}).addAction("/redirect4", (request, response) -> { }).addAction("/redirect4", (request, response) -> {
String cookie = request.getHeader(Header.COOKIE); final String cookie = request.getHeader(Header.COOKIE);
Console.log(cookie); Console.log(cookie);
response.sendOk(); response.sendOk();
}).start(); }).start();