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 0e7de4d6c..d0a8ffab7 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -8,7 +8,6 @@ import cn.hutool.core.io.resource.FileResource; import cn.hutool.core.io.resource.MultiFileResource; import cn.hutool.core.io.resource.Resource; import cn.hutool.core.lang.Assert; -import cn.hutool.core.lang.Console; import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.TableMap; import cn.hutool.core.net.ssl.SSLUtil; @@ -818,11 +817,28 @@ public class HttpRequest extends HttpBase { * 设置是否打开重定向,如果打开默认重定向次数为2
* 此方法效果与{@link #setMaxRedirectCount(int)} 一致 * + *

+ * 需要注意的是,当设置为{@code true}时,如果全局重定向次数非0,直接复用,否则设置默认2次。
+ * 当设置为{@code false}时,无论全局是否设置次数,都设置为0。
+ * 不调用此方法的情况下,使用全局默认的次数。 + *

+ * * @param isFollowRedirects 是否打开重定向 * @return this */ public HttpRequest setFollowRedirects(final boolean isFollowRedirects) { - return setMaxRedirectCount(isFollowRedirects ? 2 : 0); + if (isFollowRedirects) { + if (config.maxRedirectCount <= 0) { + // 默认两次跳转 + return setMaxRedirectCount(2); + } + } else { + // 手动强制关闭重定向,此时不受全局重定向设置影响 + if (config.maxRedirectCount < 0) { + return setMaxRedirectCount(0); + } + } + return this; } /** @@ -1021,7 +1037,7 @@ public class HttpRequest extends HttpBase { * 执行Request请求后,对响应内容后续处理
* 处理结束后关闭连接 * - * @param 结果类型 + * @param 结果类型 * @param function 响应内容处理函数 * @return 结果值 * @since 5.8.5 @@ -1230,19 +1246,18 @@ public class HttpRequest extends HttpBase { final UrlBuilder redirectUrl; String location = httpConnection.header(Header.LOCATION); - if(false == HttpUtil.isHttp(location) && false == HttpUtil.isHttps(location)){ + if (false == HttpUtil.isHttp(location) && false == HttpUtil.isHttps(location)) { // issue#I5TPSY // location可能为相对路径 - if(false == location.startsWith("/")){ + if (false == location.startsWith("/")) { location = StrUtil.addSuffixIfNot(this.url.getPathStr(), "/") + location; } redirectUrl = UrlBuilder.of(this.url.getScheme(), this.url.getHost(), this.url.getPort() , location, null, null, this.charset); - } else{ + } else { redirectUrl = UrlBuilder.ofHttpWithoutEncode(location); } - Console.log(redirectUrl); setUrl(redirectUrl); if (redirectCount < config.maxRedirectCount) { redirectCount++; diff --git a/hutool-http/src/test/java/cn/hutool/http/DownloadTest.java b/hutool-http/src/test/java/cn/hutool/http/DownloadTest.java index 390205d00..f66d7e278 100644 --- a/hutool-http/src/test/java/cn/hutool/http/DownloadTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/DownloadTest.java @@ -9,6 +9,9 @@ import org.junit.Ignore; import org.junit.Test; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.UUID; /** @@ -188,4 +191,15 @@ public class DownloadTest { FileUtil.del(file1); } } + + @Test + //@Ignore + public void downloadTeamViewerTest() throws IOException { + // 此URL有3次重定向, 需要请求4次 + final String url = "https://download.teamviewer.com/download/TeamViewer_Setup_x64.exe"; + HttpGlobalConfig.setMaxRedirectCount(20); + final Path temp = Files.createTempFile("tmp", ".exe"); + final File file = HttpUtil.downloadFileFromUrl(url, temp.toFile()); + Console.log(file.length()); + } }