mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix redirect bug
This commit is contained in:
@@ -16,23 +16,17 @@
|
||||
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.codec.binary.Base64;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.StreamProgress;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.http.HttpGlobalConfig;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -46,31 +40,22 @@ public class DownloadTest {
|
||||
@Disabled
|
||||
public void downloadPicTest() {
|
||||
final String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0";
|
||||
HttpDownloader.downloadFile(url, new File("e:/shape/t3.jpg"));
|
||||
HttpDownloader.of(url).downloadFile(new File("d:/test/download/t3.jpg"));
|
||||
Console.log("ok");
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadSizeTest() {
|
||||
final String url = "https://res.t-io.org/im/upload/img/67/8948/1119501/88097554/74541310922/85/231910/366466 - 副本.jpg";
|
||||
ClientEngineFactory.getEngine().send(Request.of(url)).body().write("e:/shape/366466.jpg");
|
||||
//HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().body().write("e:/shape/366466.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadTest1() {
|
||||
final File size = HttpDownloader.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", new File("e:/temp/"));
|
||||
System.out.println("Download size: " + size);
|
||||
final File size = HttpDownloader.of("http://explorer.bbfriend.com/crossdomain.xml").downloadFile(new File("d:test/download/temp/"));
|
||||
Console.log("Download size: " + size);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadTest() {
|
||||
// 带进度显示的文件下载
|
||||
HttpDownloader.downloadFile("http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso", FileUtil.file("d:/"), -1, new StreamProgress() {
|
||||
HttpDownloader.of("http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso").setStreamProgress(new StreamProgress() {
|
||||
|
||||
final long time = System.currentTimeMillis();
|
||||
|
||||
@@ -89,13 +74,14 @@ public class DownloadTest {
|
||||
public void finish() {
|
||||
Console.log("下载完成!");
|
||||
}
|
||||
});
|
||||
}).downloadFile(FileUtil.file("d:/test/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadFileFromUrlTest1() {
|
||||
final File file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", new File("d:/download/temp"));
|
||||
final File file = HttpDownloader.of("http://groovy-lang.org/changelogs/changelog-3.0.5.html")
|
||||
.downloadFile(new File("d:/download/temp"));
|
||||
Assertions.assertNotNull(file);
|
||||
Assertions.assertTrue(file.isFile());
|
||||
Assertions.assertTrue(file.length() > 0);
|
||||
@@ -106,7 +92,8 @@ public class DownloadTest {
|
||||
public void downloadFileFromUrlTest2() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpDownloader.downloadFile("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), 1, new StreamProgress() {
|
||||
file = HttpDownloader.of("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar")
|
||||
.setStreamProgress(new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
@@ -121,15 +108,13 @@ public class DownloadTest {
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
}).downloadFile(FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assertions.assertNotNull(file);
|
||||
Assertions.assertTrue(file.exists());
|
||||
Assertions.assertTrue(file.isFile());
|
||||
Assertions.assertTrue(file.length() > 0);
|
||||
Assertions.assertFalse(file.getName().isEmpty());
|
||||
} catch (final Exception e) {
|
||||
Assertions.assertInstanceOf(IORuntimeException.class, e);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
@@ -140,7 +125,7 @@ public class DownloadTest {
|
||||
public void downloadFileFromUrlTest3() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpDownloader.downloadFile("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), -1, new StreamProgress() {
|
||||
file = HttpDownloader.of("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar").setStreamProgress(new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
@@ -155,7 +140,7 @@ public class DownloadTest {
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
}).downloadFile(FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assertions.assertNotNull(file);
|
||||
Assertions.assertTrue(file.exists());
|
||||
@@ -172,15 +157,14 @@ public class DownloadTest {
|
||||
public void downloadFileFromUrlTest4() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
|
||||
file = HttpDownloader.of("http://groovy-lang.org/changelogs/changelog-3.0.5.html")
|
||||
.downloadFile(FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assertions.assertNotNull(file);
|
||||
Assertions.assertTrue(file.exists());
|
||||
Assertions.assertTrue(file.isFile());
|
||||
Assertions.assertTrue(file.length() > 0);
|
||||
Assertions.assertFalse(file.getName().isEmpty());
|
||||
} catch (final Exception e) {
|
||||
Assertions.assertInstanceOf(IORuntimeException.class, e);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
@@ -192,7 +176,8 @@ public class DownloadTest {
|
||||
public void downloadFileFromUrlTest5() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
|
||||
file = HttpDownloader.of("http://groovy-lang.org/changelogs/changelog-3.0.5.html")
|
||||
.downloadFile(FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
|
||||
|
||||
Assertions.assertNotNull(file);
|
||||
Assertions.assertTrue(file.exists());
|
||||
@@ -204,7 +189,8 @@ public class DownloadTest {
|
||||
|
||||
File file1 = null;
|
||||
try {
|
||||
file1 = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
|
||||
file1 = HttpDownloader.of("http://groovy-lang.org/changelogs/changelog-3.0.5.html")
|
||||
.downloadFile(FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assertions.assertNotNull(file1);
|
||||
Assertions.assertTrue(file1.exists());
|
||||
@@ -220,36 +206,10 @@ public class DownloadTest {
|
||||
public void downloadTeamViewerTest() throws IOException {
|
||||
// 此URL有3次重定向, 需要请求4次
|
||||
final String url = "https://download.teamviewer.com/download/TeamViewer_Setup_x64.exe";
|
||||
HttpGlobalConfig.setMaxRedirects(20);
|
||||
final Path temp = Files.createTempFile("tmp", ".exe");
|
||||
final File file = HttpDownloader.downloadFile(url, temp.toFile());
|
||||
Console.log(file.length());
|
||||
}
|
||||
HttpGlobalConfig.setMaxRedirects(2);
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadToStreamTest() {
|
||||
String url2 = "http://storage.chancecloud.com.cn/20200413_%E7%B2%A4B12313_386.pdf";
|
||||
final ByteArrayOutputStream os2 = new ByteArrayOutputStream();
|
||||
HttpDownloader.download(url2, os2, false, null);
|
||||
|
||||
url2 = "http://storage.chancecloud.com.cn/20200413_粤B12313_386.pdf";
|
||||
HttpDownloader.download(url2, os2, false, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadStringTest() {
|
||||
final String url = "https://www.baidu.com";
|
||||
// 从远程直接读取字符串,需要自定义编码,直接调用JDK方法
|
||||
final String content2 = HttpDownloader.downloadString(url, CharsetUtil.UTF_8, null);
|
||||
Console.log(content2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void gimg2Test(){
|
||||
final byte[] bytes = HttpDownloader.downloadBytes("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H320120Z3%2F200H3120Z3-6-1200.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621996490&t=8c384c2823ea453da15a1b9cd5183eea");
|
||||
Console.log(Base64.encode(bytes));
|
||||
final Response send = Request.of(url).send(ClientEngineFactory.createEngine("jdkClient"));
|
||||
Console.log(send.getStatus());
|
||||
Console.log(send.headers());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user