修复Http重定全局设置无效问题

This commit is contained in:
Looly 2022-09-30 19:23:47 +08:00
parent 44684aa036
commit 85a6df75b7
3 changed files with 30 additions and 11 deletions

View File

@ -12,6 +12,7 @@
### 🐞Bug修复 ### 🐞Bug修复
* 【poi 】 修复ExcelReader读取只有标题行报错问题issue#I5U1JA@Gitee * 【poi 】 修复ExcelReader读取只有标题行报错问题issue#I5U1JA@Gitee
* 【http 】 修复Http重定向时相对路径导致的问题issue#I5TPSY@Gitee * 【http 】 修复Http重定向时相对路径导致的问题issue#I5TPSY@Gitee
* 【http 】 修复Http重定全局设置无效问题pr#827@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -832,7 +832,11 @@ public class HttpRequest extends HttpBase<HttpRequest> {
* @return this * @return this
*/ */
public HttpRequest setFollowRedirects(boolean isFollowRedirects) { public HttpRequest setFollowRedirects(boolean isFollowRedirects) {
return setMaxRedirectCount(isFollowRedirects ? 2 : 0); if(isFollowRedirects && config.maxRedirectCount <= 0){
// 默认两次跳转
return setMaxRedirectCount(2);
}
return this;
} }
/** /**

View File

@ -9,6 +9,9 @@ import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
/** /**
@ -21,7 +24,7 @@ public class DownloadTest {
@Test @Test
@Ignore @Ignore
public void downloadPicTest() { 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"); HttpUtil.downloadFile(url, "e:/pic/t3.jpg");
Console.log("ok"); Console.log("ok");
} }
@ -29,14 +32,14 @@ public class DownloadTest {
@Test @Test
@Ignore @Ignore
public void downloadSizeTest() { 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"); HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().writeBody("e:/pic/366466.jpg");
} }
@Test @Test
@Ignore @Ignore
public void downloadTest1() { 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); System.out.println("Download size: " + size);
} }
@ -54,8 +57,8 @@ public class DownloadTest {
} }
@Override @Override
public void progress(long contentLength, long progressSize) { public void progress(final long contentLength, final long progressSize) {
long speed = progressSize / (System.currentTimeMillis() - time) * 1000; final long speed = progressSize / (System.currentTimeMillis() - time) * 1000;
Console.log("总大小:{}, 已下载:{}, 速度:{}/s", FileUtil.readableFileSize(contentLength), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed)); Console.log("总大小:{}, 已下载:{}, 速度:{}/s", FileUtil.readableFileSize(contentLength), FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed));
} }
@ -69,7 +72,7 @@ public class DownloadTest {
@Test @Test
@Ignore @Ignore
public void downloadFileFromUrlTest1() { 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.assertNotNull(file);
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.length() > 0);
@ -87,7 +90,7 @@ public class DownloadTest {
} }
@Override @Override
public void progress(long contentLength, long progressSize) { public void progress(final long contentLength, final long progressSize) {
System.out.println("download size:" + progressSize); System.out.println("download size:" + progressSize);
} }
@ -102,7 +105,7 @@ public class DownloadTest {
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.length() > 0);
Assert.assertTrue(file.getName().length() > 0); Assert.assertTrue(file.getName().length() > 0);
} catch (Exception e) { } catch (final Exception e) {
Assert.assertTrue(e instanceof IORuntimeException); Assert.assertTrue(e instanceof IORuntimeException);
} finally { } finally {
FileUtil.del(file); FileUtil.del(file);
@ -121,7 +124,7 @@ public class DownloadTest {
} }
@Override @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); System.out.println("contentLength:" + contentLength + "download size:" + progressSize);
} }
@ -153,7 +156,7 @@ public class DownloadTest {
Assert.assertTrue(file.isFile()); Assert.assertTrue(file.isFile());
Assert.assertTrue(file.length() > 0); Assert.assertTrue(file.length() > 0);
Assert.assertTrue(file.getName().length() > 0); Assert.assertTrue(file.getName().length() > 0);
} catch (Exception e) { } catch (final Exception e) {
Assert.assertTrue(e instanceof IORuntimeException); Assert.assertTrue(e instanceof IORuntimeException);
} finally { } finally {
FileUtil.del(file); FileUtil.del(file);
@ -188,4 +191,15 @@ public class DownloadTest {
FileUtil.del(file1); 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());
}
} }