From 85a6df75b7dd9962b6bf5b5566decb652aca8fea Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 30 Sep 2022 19:23:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHttp=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=AE=BE=E7=BD=AE=E6=97=A0=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/java/cn/hutool/http/HttpRequest.java | 6 +++- .../java/cn/hutool/http/DownloadTest.java | 34 +++++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 212d7e2b2..143df2f7d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### 🐞Bug修复 * 【poi 】 修复ExcelReader读取只有标题行报错问题(issue#I5U1JA@Gitee) * 【http 】 修复Http重定向时相对路径导致的问题(issue#I5TPSY@Gitee) +* 【http 】 修复Http重定全局设置无效问题(pr#827@Github) ------------------------------------------------------------------------------------------------------------- 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 496e9bdc5..82ae49e01 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -832,7 +832,11 @@ public class HttpRequest extends HttpBase { * @return this */ public HttpRequest setFollowRedirects(boolean isFollowRedirects) { - return setMaxRedirectCount(isFollowRedirects ? 2 : 0); + if(isFollowRedirects && config.maxRedirectCount <= 0){ + // 默认两次跳转 + return setMaxRedirectCount(2); + } + return this; } /** 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 2004d745b..888f5d765 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; /** @@ -21,7 +24,7 @@ public class DownloadTest { @Test @Ignore public void downloadPicTest() { - String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0"; + final String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0"; HttpUtil.downloadFile(url, "e:/pic/t3.jpg"); Console.log("ok"); } @@ -29,14 +32,14 @@ public class DownloadTest { @Test @Ignore public void downloadSizeTest() { - String url = "https://res.t-io.org/im/upload/img/67/8948/1119501/88097554/74541310922/85/231910/366466 - 副本.jpg"; + final String url = "https://res.t-io.org/im/upload/img/67/8948/1119501/88097554/74541310922/85/231910/366466 - 副本.jpg"; HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().writeBody("e:/pic/366466.jpg"); } @Test @Ignore public void downloadTest1() { - long size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/"); + final long size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/"); System.out.println("Download size: " + size); } @@ -54,8 +57,8 @@ public class DownloadTest { } @Override - public void progress(long contentLength, long progressSize) { - long speed = progressSize / (System.currentTimeMillis() - time) * 1000; + public void progress(final long contentLength, final long progressSize) { + final long speed = progressSize / (System.currentTimeMillis() - time) * 1000; Console.log("总大小:{}, 已下载:{}, 速度:{}/s", FileUtil.readableFileSize(contentLength), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed)); } @@ -69,7 +72,7 @@ public class DownloadTest { @Test @Ignore public void downloadFileFromUrlTest1() { - File file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", "d:/download/temp"); + final File file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", "d:/download/temp"); Assert.assertNotNull(file); Assert.assertTrue(file.isFile()); Assert.assertTrue(file.length() > 0); @@ -87,7 +90,7 @@ public class DownloadTest { } @Override - public void progress(long contentLength, long progressSize) { + public void progress(final long contentLength, final long progressSize) { System.out.println("download size:" + progressSize); } @@ -102,7 +105,7 @@ public class DownloadTest { Assert.assertTrue(file.isFile()); Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.getName().length() > 0); - } catch (Exception e) { + } catch (final Exception e) { Assert.assertTrue(e instanceof IORuntimeException); } finally { FileUtil.del(file); @@ -121,7 +124,7 @@ public class DownloadTest { } @Override - public void progress(long contentLength, long progressSize) { + public void progress(final long contentLength, final long progressSize) { System.out.println("contentLength:" + contentLength + "download size:" + progressSize); } @@ -153,7 +156,7 @@ public class DownloadTest { Assert.assertTrue(file.isFile()); Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.getName().length() > 0); - } catch (Exception e) { + } catch (final Exception e) { Assert.assertTrue(e instanceof IORuntimeException); } finally { FileUtil.del(file); @@ -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()); + } }