diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47667d384..fc8f5ed76 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
### 🐣新特性
### 🐞Bug修复
+* 【http 】 HttpUtil重定向次数失效问题(issue#I4O28Q@Gitee)
-------------------------------------------------------------------------------------------------------------
# 5.7.18 (2021-12-25)
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
index ae76d932d..5cff0e96d 100755
--- a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
@@ -20,9 +20,10 @@ import java.net.HttpURLConnection;
public class HttpGlobalConfig implements Serializable {
private static final long serialVersionUID = 1L;
- protected static int timeout = -1;
+ private static int timeout = -1;
private static boolean isAllowPatch = false;
private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16);
+ private static int maxRedirectCount = 0;
/**
* 获取全局默认的超时时长
@@ -62,6 +63,28 @@ public class HttpGlobalConfig implements Serializable {
boundary = customBoundary;
}
+ /**
+ * 获取全局默认的最大重定向次数,如设置0表示不重定向
+ * 如果设置为1,表示重定向一次,即请求两次
+ *
+ * @return 全局默认的最大重定向次数
+ * @since 5.7.19
+ */
+ public static int getMaxRedirectCount() {
+ return maxRedirectCount;
+ }
+
+ /**
+ * 设置默认全局默认的最大重定向次数,如设置0表示不重定向
+ * 如果设置为1,表示重定向一次,即请求两次
+ *
+ * @param customMaxRedirectCount 全局默认的最大重定向次数
+ * @since 5.7.19
+ */
+ synchronized public static void setMaxRedirectCount(int customMaxRedirectCount) {
+ maxRedirectCount = customMaxRedirectCount;
+ }
+
/**
* 获取Cookie管理器,用于自定义Cookie管理
*
@@ -80,7 +103,7 @@ public class HttpGlobalConfig implements Serializable {
* @since 4.5.14
* @see GlobalCookieManager#setCookieManager(CookieManager)
*/
- public static void setCookieManager(CookieManager customCookieManager) {
+ synchronized public static void setCookieManager(CookieManager customCookieManager) {
GlobalCookieManager.setCookieManager(customCookieManager);
}
@@ -90,7 +113,7 @@ public class HttpGlobalConfig implements Serializable {
* @since 4.1.9
* @see GlobalCookieManager#setCookieManager(CookieManager)
*/
- public static void closeCookie() {
+ synchronized public static void closeCookie() {
GlobalCookieManager.setCookieManager(null);
}
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 7f07ff710..b971c60e2 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java
@@ -206,11 +206,11 @@ public class HttpRequest extends HttpBase {
/**
* 默认连接超时
*/
- private int connectionTimeout = HttpGlobalConfig.timeout;
+ private int connectionTimeout = HttpGlobalConfig.getTimeout();
/**
* 默认读取超时
*/
- private int readTimeout = HttpGlobalConfig.timeout;
+ private int readTimeout = HttpGlobalConfig.getTimeout();
/**
* 存储表单数据
*/
@@ -243,7 +243,7 @@ public class HttpRequest extends HttpBase {
/**
* 最大重定向次数
*/
- private int maxRedirectCount;
+ private int maxRedirectCount = HttpGlobalConfig.getMaxRedirectCount();
/**
* Chuncked块大小,0或小于0表示不设置Chuncked模式
*/
@@ -1131,8 +1131,8 @@ public class HttpRequest extends HttpBase {
.setReadTimeout(this.readTimeout)//
.setMethod(this.method)//
.setHttpsInfo(this.hostnameVerifier, this.ssf)//
- // 定义转发
- .setInstanceFollowRedirects(this.maxRedirectCount > 0)
+ // 关闭JDK自动转发,采用手动转发方式
+ .setInstanceFollowRedirects(false)
// 流方式上传数据
.setChunkedStreamingMode(this.blockSize)
// 覆盖默认Header
@@ -1174,13 +1174,8 @@ public class HttpRequest extends HttpBase {
* @return {@link HttpResponse},无转发返回 {@code null}
*/
private HttpResponse sendRedirectIfPossible(boolean isAsync) {
- if (this.maxRedirectCount < 1) {
- // 不重定向
- return null;
- }
-
// 手动实现重定向
- if (this.httpConnection.getHttpURLConnection().getInstanceFollowRedirects()) {
+ if (this.maxRedirectCount > 0) {
int responseCode;
try {
responseCode = httpConnection.responseCode();
@@ -1195,6 +1190,7 @@ public class HttpRequest extends HttpBase {
setUrl(httpConnection.header(Header.LOCATION));
if (redirectCount < this.maxRedirectCount) {
redirectCount++;
+ // 重定向不再走过滤器
return doExecute(isAsync, null);
}
}
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
index ffe7ed669..a6904f6f4 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
@@ -130,7 +130,7 @@ public class HttpUtil {
* @return 返回内容,如果只检查状态码,正常只返回 "",不正常返回 null
*/
public static String get(String urlString) {
- return get(urlString, HttpGlobalConfig.timeout);
+ return get(urlString, HttpGlobalConfig.getTimeout());
}
/**
@@ -177,7 +177,7 @@ public class HttpUtil {
* @return 返回数据
*/
public static String post(String urlString, Map paramMap) {
- return post(urlString, paramMap, HttpGlobalConfig.timeout);
+ return post(urlString, paramMap, HttpGlobalConfig.getTimeout());
}
/**
@@ -207,7 +207,7 @@ public class HttpUtil {
* @return 返回数据
*/
public static String post(String urlString, String body) {
- return post(urlString, body, HttpGlobalConfig.timeout);
+ return post(urlString, body, HttpGlobalConfig.getTimeout());
}
/**
diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
index c1cb54e9d..1c5e32d89 100644
--- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
+++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java
@@ -149,4 +149,23 @@ public class HttpRequestTest {
Console.log(execute.body());
}
+ @Test
+ @Ignore
+ public void followRedirectsTest(){
+ // 从5.7.19开始关闭JDK的自动重定向功能,改为手动重定向
+ // 当有多层重定向时,JDK的重定向会失效,或者说只有最后一个重定向有效,因此改为手动更易控制次数
+ // 此链接有两次重定向,当设置次数为1时,表示最多执行一次重定向,即请求2次
+ String url = "http://api.rosysun.cn/sjtx/?type=2";
+// String url = "https://api.btstu.cn/sjtx/api.php?lx=b1";
+
+ // 方式1:全局设置
+ HttpGlobalConfig.setMaxRedirectCount(1);
+ HttpResponse execute = HttpRequest.get(url).execute();
+ Console.log(execute.getStatus(), execute.header(Header.LOCATION));
+
+ // 方式2,单独设置
+ execute = HttpRequest.get(url).setMaxRedirectCount(1).execute();
+ Console.log(execute.getStatus(), execute.header(Header.LOCATION));
+ }
+
}