This commit is contained in:
Looly
2022-11-30 23:51:36 +08:00
parent 185cd09ab0
commit 75d536839c
34 changed files with 877 additions and 2145 deletions

View File

@@ -1,14 +1,19 @@
package cn.hutool.http;
import cn.hutool.core.codec.BaseN.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.lang.Console;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.client.HttpDownloader;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.engine.ClientEngineFactory;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@@ -26,7 +31,7 @@ public class DownloadTest {
@Ignore
public void downloadPicTest() {
final String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0";
HttpUtil.downloadFile(url, "e:/pic/t3.jpg");
HttpDownloader.downloadFile(url, new File("e:/pic/t3.jpg"));
Console.log("ok");
}
@@ -34,13 +39,14 @@ public class DownloadTest {
@Ignore
public void downloadSizeTest() {
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().body().write("e:/pic/366466.jpg");
ClientEngineFactory.get().send(Request.of(url)).body().write("e:/pic/366466.jpg");
//HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().body().write("e:/pic/366466.jpg");
}
@Test
@Ignore
public void downloadTest1() {
final File size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/");
final File size = HttpDownloader.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", new File("e:/temp/"));
System.out.println("Download size: " + size);
}
@@ -48,7 +54,7 @@ public class DownloadTest {
@Ignore
public void downloadTest() {
// 带进度显示的文件下载
HttpUtil.downloadFile("http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso", FileUtil.file("d:/"), new StreamProgress() {
HttpDownloader.downloadFile("http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso", FileUtil.file("d:/"), -1, new StreamProgress() {
final long time = System.currentTimeMillis();
@@ -73,7 +79,7 @@ public class DownloadTest {
@Test
@Ignore
public void downloadFileFromUrlTest1() {
final File file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", "d:/download/temp");
final File file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", new File("d:/download/temp"));
Assert.assertNotNull(file);
Assert.assertTrue(file.isFile());
Assert.assertTrue(file.length() > 0);
@@ -84,7 +90,7 @@ public class DownloadTest {
public void downloadFileFromUrlTest2() {
File file = null;
try {
file = HttpUtil.downloadFileFromUrl("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.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() {
@Override
public void start() {
System.out.println("start");
@@ -118,7 +124,7 @@ public class DownloadTest {
public void downloadFileFromUrlTest3() {
File file = null;
try {
file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), new StreamProgress() {
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() {
@Override
public void start() {
System.out.println("start");
@@ -150,7 +156,7 @@ public class DownloadTest {
public void downloadFileFromUrlTest4() {
File file = null;
try {
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
Assert.assertNotNull(file);
Assert.assertTrue(file.exists());
@@ -170,7 +176,7 @@ public class DownloadTest {
public void downloadFileFromUrlTest5() {
File file = null;
try {
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
file = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
Assert.assertNotNull(file);
Assert.assertTrue(file.exists());
@@ -182,7 +188,7 @@ public class DownloadTest {
File file1 = null;
try {
file1 = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
file1 = HttpDownloader.downloadFile("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
Assert.assertNotNull(file1);
Assert.assertTrue(file1.exists());
@@ -200,7 +206,34 @@ public class DownloadTest {
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());
final File file = HttpDownloader.downloadFile(url, temp.toFile());
Console.log(file.length());
}
@Test
@Ignore
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
@Ignore
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
@Ignore
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));
}
}

View File

@@ -4,11 +4,11 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.net.ssl.SSLProtocols;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.http.client.engine.jdk.HttpResponse;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.Response;
import cn.hutool.http.meta.Header;
import cn.hutool.http.meta.Method;
import org.junit.Ignore;
@@ -20,7 +20,7 @@ import java.util.List;
import java.util.Map;
/**
* {@link HttpRequest}单元测试
* {@link Request}单元测试
*
* @author Looly
*/
@@ -28,18 +28,11 @@ import java.util.Map;
public class HttpRequestTest {
final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2";
@Test
@Ignore
public void getHttpsTest() {
final String body = HttpRequest.get("https://www.hutool.cn/").timeout(10).execute().bodyStr();
Console.log(body);
}
@Test
@Ignore
public void getHttpsThenTest() {
HttpRequest
.get("https://hutool.cn")
Request.of("https://hutool.cn")
.send()
.then(response -> Console.log(response.body()));
}
@@ -47,9 +40,9 @@ public class HttpRequestTest {
@Ignore
public void getCookiesTest() {
// 检查在Connection关闭情况下Cookie是否可以正常获取
final HttpResponse res = HttpRequest.get("https://www.oschina.net/").execute();
final Response res = Request.of("https://www.oschina.net/").send();
final String body = res.bodyStr();
Console.log(res.getCookies());
Console.log(res.getCookieStr());
Console.log(body);
}
@@ -58,14 +51,14 @@ public class HttpRequestTest {
public void toStringTest() {
final String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
final HttpRequest request = HttpRequest.get(url).body("a=乌海");
final Request request = Request.of(url).body("a=乌海");
Console.log(request.toString());
}
@Test
@Ignore
public void asyncHeadTest() {
final HttpResponse response = HttpRequest.head(url).execute();
final Response response = Request.of(url).method(Method.HEAD).send();
final Map<String, List<String>> headers = response.headers();
Console.log(headers);
Console.log(response.body());
@@ -76,7 +69,7 @@ public class HttpRequestTest {
public void asyncGetTest() {
final StopWatch timer = DateUtil.createStopWatch();
timer.start();
final HttpResponse body = HttpRequest.get(url).charset("GBK").executeAsync();
final Response body = Request.of(url).charset(CharsetUtil.GBK).send();
timer.stop();
final long interval = timer.getLastTaskTimeMillis();
timer.start();
@@ -91,7 +84,7 @@ public class HttpRequestTest {
public void syncGetTest() {
final StopWatch timer = DateUtil.createStopWatch();
timer.start();
final HttpResponse body = HttpRequest.get(url).charset("GBK").execute();
final Response body = Request.of(url).charset(CharsetUtil.GBK).send();
timer.stop();
final long interval = timer.getLastTaskTimeMillis();
@@ -102,26 +95,12 @@ public class HttpRequestTest {
Console.log("Async response spend {}ms, body spend {}ms", interval, interval2);
}
@Test
@Ignore
public void customGetTest() {
// 自定义构建HTTP GET请求发送Http GET请求针对HTTPS安全加密可以自定义SSL
final HttpRequest request = HttpRequest.get(url)
// 自定义返回编码
.charset(CharsetUtil.GBK)
// 禁用缓存
.disableCache()
// 自定义SSL版本
.setSSLProtocol(SSLProtocols.TLSv12);
Console.log(request.execute().body());
}
@Test
@Ignore
public void getDeflateTest() {
final HttpResponse res = HttpRequest.get("https://comment.bilibili.com/67573272.xml")
final Response res = Request.of("https://comment.bilibili.com/67573272.xml")
.header(Header.ACCEPT_ENCODING, "deflate")
.execute();
.send();
Console.log(res.header(Header.CONTENT_ENCODING));
Console.log(res.body());
}
@@ -129,7 +108,7 @@ public class HttpRequestTest {
@Test
@Ignore
public void bodyTest() {
final String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().bodyStr();
final String ddddd1 = Request.of("https://baijiahao.baidu.com/s").body("id=1625528941695652600").send().bodyStr();
Console.log(ddddd1);
}
@@ -149,9 +128,9 @@ public class HttpRequestTest {
map.put("size", "2");
map.put("sizes", list);
HttpRequest
.get("http://localhost:8888/get")
.form(map)
Request
.of("http://localhost:8888/get")
.form(map).send()
.then(resp -> Console.log(resp.body()));
}
@@ -159,9 +138,9 @@ public class HttpRequestTest {
@Ignore
public void getWithoutEncodeTest() {
final String url = "https://img-cloud.voc.com.cn/140/2020/09/03/c3d41b93e0d32138574af8e8b50928b376ca5ba61599127028157.png?imageMogr2/auto-orient/thumbnail/500&pid=259848";
final HttpRequest get = HttpUtil.createGet(url);
Console.log(get.getUrl());
final HttpResponse execute = get.execute();
final Request get = Request.of(url);
Console.log(get.url());
final Response execute = get.send();
Console.log(execute.body());
}
@@ -176,11 +155,11 @@ public class HttpRequestTest {
// 方式1全局设置
HttpGlobalConfig.setMaxRedirectCount(1);
HttpResponse execute = HttpRequest.get(url).execute();
Response execute = Request.of(url).send();
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
// 方式2单独设置
execute = HttpRequest.get(url).setMaxRedirectCount(1).execute();
execute = Request.of(url).setMaxRedirectCount(1).send();
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
}
@@ -190,8 +169,8 @@ public class HttpRequestTest {
final String url = "https://postman-echo.com/get";
final Map<String, Object> map = new HashMap<>();
map.put("aaa", "application+1@qqq.com");
final HttpRequest request =HttpUtil.createGet(url).form(map);
Console.log(request.execute().body());
final Request request =Request.of(url).form(map);
Console.log(request.send().body());
}
@Test
@@ -200,51 +179,51 @@ public class HttpRequestTest {
final UrlBuilder urlBuilder = new UrlBuilder();
urlBuilder.setScheme("https").setHost("hutool.cn");
final HttpRequest httpRequest = new HttpRequest(urlBuilder);
httpRequest.method(Method.GET).execute();
final Request httpRequest = Request.of(urlBuilder);
httpRequest.method(Method.GET).send();
}
@Test
@Ignore
public void getCookieTest(){
final HttpResponse execute = HttpRequest.get("http://localhost:8888/getCookier").execute();
Console.log(execute.getCookies());
final Response execute = Request.of("http://localhost:8888/getCookier").send();
Console.log(execute.getCookieStr());
}
@Test
public void optionsTest() {
final HttpRequest options = HttpRequest.options("https://hutool.cn");
final Request options = Request.of("https://hutool.cn").method(Method.OPTIONS);
Assert.notNull(options.toString());
}
@Test
public void deleteTest() {
final HttpRequest options = HttpRequest.delete("https://hutool.cn");
final Request options = Request.of("https://hutool.cn").method(Method.DELETE);
Assert.notNull(options.toString());
}
@Test
public void traceTest() {
final HttpRequest options = HttpRequest.trace("https://hutool.cn");
final Request options = Request.of("https://hutool.cn").method(Method.TRACE);
Assert.notNull(options.toString());
}
@Test
public void getToStringTest() {
final HttpRequest a = HttpRequest.get("https://hutool.cn/").form("a", 1);
final Request a = Request.of("https://hutool.cn/").form(MapUtil.of("a", 1));
Assert.notNull(a.toString());
}
@Test
public void postToStringTest() {
final HttpRequest a = HttpRequest.post("https://hutool.cn/").form("a", 1);
final Request a = Request.of("https://hutool.cn/").method(Method.POST).form(MapUtil.of("a", 1));
Console.log(a.toString());
}
@Test
@Ignore
public void issueI5Y68WTest() {
final HttpResponse httpResponse = HttpRequest.get("http://82.157.17.173:8100/app/getAddress").execute();
final Response httpResponse = Request.of("http://82.157.17.173:8100/app/getAddress").send();
Console.log(httpResponse.body());
}
}

View File

@@ -1,18 +1,16 @@
package cn.hutool.http;
import cn.hutool.core.codec.BaseN.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.regex.ReUtil;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.client.Request;
import cn.hutool.http.meta.Header;
import cn.hutool.http.meta.Method;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -35,23 +33,13 @@ public class HttpUtilTest {
Assert.assertFalse(HttpUtil.isHttps("ftp://aaa.bbb"));
}
@Test
@Ignore
public void postTest() {
final String result = HttpUtil.createPost("api.uhaozu.com/goods/description/1120448506")
.charset(CharsetUtil.NAME_UTF_8)
.execute().bodyStr();
Console.log(result);
}
@Test
@Ignore
public void postTest2() {
// 某些接口对Accept头有特殊要求此处自定义头
final String result = HttpUtil
.createPost("http://cmp.ishanghome.com/cmp/v1/community/queryClusterCommunity")
.header(Header.ACCEPT, "*/*")
.execute()
final String result = HttpUtil.send(Request
.of("http://cmp.ishanghome.com/cmp/v1/community/queryClusterCommunity")
.header(Header.ACCEPT, "*/*"))
.bodyStr();
Console.log(result);
}
@@ -68,9 +56,9 @@ public class HttpUtilTest {
public void getTest2() {
// 此链接较为特殊User-Agent去掉后进入一个JS跳转页面如果设置了需要开启302跳转
// 自定义的默认header无效
final String result = HttpRequest
.get("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101457313&redirect_uri=http%3A%2F%2Fwww.benmovip.com%2Fpay-cloud%2Fqqlogin%2FgetCode&state=ok")
.removeHeader(Header.USER_AGENT).execute().bodyStr();
final String result = Request
.of("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101457313&redirect_uri=http%3A%2F%2Fwww.benmovip.com%2Fpay-cloud%2Fqqlogin%2FgetCode&state=ok")
.header(Header.USER_AGENT, null).send().bodyStr();
Console.log(result);
}
@@ -86,38 +74,17 @@ public class HttpUtilTest {
@Ignore
public void getTest4() {
// 测试url中带有空格的情况
final byte[] str = HttpRequest.get("http://img01.fs.yiban.cn/mobile/2D0Y71").execute().bodyBytes();
final byte[] str = Request.of("http://img01.fs.yiban.cn/mobile/2D0Y71").send().bodyBytes();
FileUtil.writeBytes(str, "f:/test/2D.jpg");
Console.log(str);
}
@Test
@Ignore
public void getTest5() {
String url2 = "http://storage.chancecloud.com.cn/20200413_%E7%B2%A4B12313_386.pdf";
final ByteArrayOutputStream os2 = new ByteArrayOutputStream();
HttpUtil.download(url2, os2, false);
url2 = "http://storage.chancecloud.com.cn/20200413_粤B12313_386.pdf";
HttpUtil.download(url2, os2, false);
}
@Test
@Ignore
public void get12306Test() {
HttpRequest.get("https://kyfw.12306.cn/otn/")
.setFollowRedirects(true)
.then(response -> Console.log(response.body()));
}
@Test
@Ignore
public void downloadStringTest() {
final String url = "https://www.baidu.com";
// 从远程直接读取字符串需要自定义编码直接调用JDK方法
final String content2 = HttpUtil.downloadString(url, CharsetUtil.NAME_UTF_8);
Console.log(content2);
HttpUtil.send(Request.of("https://kyfw.12306.cn/otn/").setMaxRedirectCount(2))
.then(response -> Console.log(response.bodyStr()));
}
@Test
@@ -138,141 +105,10 @@ public class HttpUtilTest {
}
@Test
public void decodeParamsTest() {
final String paramsStr = "uuuu=0&a=b&c=%3F%23%40!%24%25%5E%26%3Ddsssss555555";
final Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("0", map.get("uuuu").get(0));
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("?#@!$%^&=dsssss555555", map.get("c").get(0));
}
@Test
public void decodeParamMapTest() {
// 参数值存在分界标记等号时
final Map<String, String> paramMap = HttpUtil.decodeParamMap("https://www.xxx.com/api.action?aa=123&f_token=NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=", CharsetUtil.UTF_8);
Assert.assertEquals("123",paramMap.get("aa"));
Assert.assertEquals("NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=",paramMap.get("f_token"));
}
@Test
public void toParamsTest() {
final String paramsStr = "uuuu=0&a=b&c=3Ddsssss555555";
final Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.NAME_UTF_8);
final String encodedParams = HttpUtil.toParams(map);
Assert.assertEquals(paramsStr, encodedParams);
}
@Test
public void encodeParamTest() {
// ?单独存在去除之,&单位位于末尾去除之
String paramsStr = "?a=b&c=d&";
String encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=b&c=d", encode);
// url不参与转码
paramsStr = "http://www.abc.dd?a=b&c=d&";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("http://www.abc.dd?a=b&c=d", encode);
// b=b中的=被当作值的一部分不做encode
paramsStr = "a=b=b&c=d&";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=b=b&c=d", encode);
// =d的情况被处理为key为空
paramsStr = "a=bbb&c=d&=d";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=bbb&c=d&=d", encode);
// d=的情况被处理为value为空
paramsStr = "a=bbb&c=d&d=";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=bbb&c=d&d=", encode);
// 多个&&被处理为单个,相当于空条件
paramsStr = "a=bbb&c=d&&&d=";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=bbb&c=d&d=", encode);
// &d&相当于只有键,无值得情况
paramsStr = "a=bbb&c=d&d&";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=bbb&c=d&d=", encode);
// 中文的键和值被编码
paramsStr = "a=bbb&c=你好&哈喽&";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("a=bbb&c=%E4%BD%A0%E5%A5%BD&%E5%93%88%E5%96%BD=", encode);
// URL原样输出
paramsStr = "https://www.hutool.cn/";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals(paramsStr, encode);
// URL原样输出
paramsStr = "https://www.hutool.cn/?";
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.UTF_8);
Assert.assertEquals("https://www.hutool.cn/", encode);
}
@Test
public void decodeParamTest() {
// 开头的?被去除
String a = "?a=b&c=d&";
Map<String, List<String>> map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
// =e被当作空为keye为value
a = "?a=b&c=d&=e";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
Assert.assertEquals("e", map.get("").get(0));
// 多余的&去除
a = "?a=b&c=d&=e&&&&";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
Assert.assertEquals("e", map.get("").get(0));
// 值为空
a = "?a=b&c=d&e=";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
Assert.assertEquals("", map.get("e").get(0));
// &=被作为键和值都为空
a = "a=b&c=d&=";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
Assert.assertEquals("", map.get("").get(0));
// &e&这类单独的字符串被当作key
a = "a=b&c=d&e&";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("b", map.get("a").get(0));
Assert.assertEquals("d", map.get("c").get(0));
Assert.assertNull(map.get("e").get(0));
Assert.assertNull(map.get("").get(0));
// 被编码的键和值被还原
a = "a=bbb&c=%E4%BD%A0%E5%A5%BD&%E5%93%88%E5%96%BD=";
map = HttpUtil.decodeParams(a, CharsetUtil.NAME_UTF_8);
Assert.assertEquals("bbb", map.get("a").get(0));
Assert.assertEquals("你好", map.get("c").get(0));
Assert.assertEquals("", map.get("哈喽").get(0));
}
@Test
@Ignore
//@Ignore
public void patchTest() {
// 验证patch请求是否可用
final String body = HttpRequest.patch("https://www.baidu.com").execute().bodyStr();
final String body = HttpUtil.send(Request.of("https://hutool.cn").method(Method.PATCH)).bodyStr();
Console.log(body);
}
@@ -315,18 +151,6 @@ public class HttpUtilTest {
Assert.assertEquals("utf-8", charsetName);
}
@Test
public void normalizeParamsTest() {
final String encodeResult = HttpUtil.normalizeParams("参数", CharsetUtil.UTF_8);
Assert.assertEquals("%E5%8F%82%E6%95%B0", encodeResult);
}
@Test
public void normalizeBlankParamsTest() {
final String encodeResult = HttpUtil.normalizeParams("", CharsetUtil.UTF_8);
Assert.assertEquals("", encodeResult);
}
@Test
public void getMimeTypeTest() {
final String mimeType = HttpUtil.getMimeType("aaa.aaa");
@@ -357,18 +181,10 @@ public class HttpUtilTest {
Console.log(s);
}
@Test
@Ignore
public void gimg2Test(){
final byte[] bytes = HttpUtil.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));
}
@Test
@Ignore
public void acplayTest(){
final String body = HttpRequest.get("https://api.acplay.net/api/v2/bangumi/9541")
.execute().bodyStr();
final String body = HttpUtil.send(Request.of("https://api.acplay.net/api/v2/bangumi/9541")).bodyStr();
Console.log(body);
}
@@ -378,7 +194,7 @@ public class HttpUtilTest {
HttpGlobalConfig.setDecodeUrl(false);
final String url = "https://p3-sign.douyinpic.com/tos-cn-i-0813/f41afb2e79a94dcf80970affb9a69415~noop.webp?x-expires=1647738000&x-signature=%2Br1ekUCGjXiu50Y%2Bk0MO4ovulK8%3D&from=4257465056&s=PackSourceEnum_DOUYIN_REFLOW&se=false&sh=&sc=&l=2022021809224601020810013524310DD3&biz_tag=aweme_images";
final HttpRequest request = HttpRequest.of(url).method(Method.GET);
Console.log(request.execute().body());
final String body = HttpUtil.send(Request.of(url)).bodyStr();
Console.log(body);
}
}

View File

@@ -3,8 +3,8 @@ package cn.hutool.http;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.http.client.engine.jdk.HttpResponse;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.Response;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -23,10 +23,10 @@ public class Issue2531Test {
final String queryParam = MapUtil.join(map, "&", "=");//返回str=+123
Console.log(queryParam);
final HttpRequest request = HttpUtil.createGet("http://localhost:8888/formTest?" + queryParam);
final Request request = Request.of("http://localhost:8888/formTest?" + queryParam);
//request.setUrl("http://localhost:8888/formTest" + "?" + queryParam);
//noinspection resource
final HttpResponse execute = request.execute();
final Response execute = request.send();
Console.log(execute.body());
}

View File

@@ -1,7 +1,8 @@
package cn.hutool.http;
import cn.hutool.core.lang.Console;
import cn.hutool.http.client.engine.jdk.HttpResponse;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.Response;
import cn.hutool.http.meta.Header;
import org.junit.Ignore;
import org.junit.Test;
@@ -12,10 +13,10 @@ public class IssueI5TPSYTest {
@Ignore
public void redirectTest() {
final String url = "https://bsxt.gdzwfw.gov.cn/UnifiedReporting/auth/newIndex";
final HttpResponse res = HttpUtil.createGet(url).setFollowRedirects(true)
final Response res = HttpUtil.send(Request.of(url)
.setMaxRedirectCount(2)
.header(Header.USER_AGENT, "PostmanRuntime/7.29.2")
.cookie("jsessionid=s%3ANq6YTcIHQWrHkEqOSxiQNijDMhoFNV4_.h2MVD1CkW7sOZ60OSnPs7m4K%2FhENfYy%2FdzjKvSiZF4E")
.execute();
.cookie("jsessionid=s%3ANq6YTcIHQWrHkEqOSxiQNijDMhoFNV4_.h2MVD1CkW7sOZ60OSnPs7m4K%2FhENfYy%2FdzjKvSiZF4E"));
Console.log(res.body());
}
}

View File

@@ -1,6 +1,6 @@
package cn.hutool.http;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.http.client.Request;
import cn.hutool.json.JSONUtil;
import org.junit.Ignore;
import org.junit.Test;
@@ -19,7 +19,8 @@ public class IssueI5WAV4Test {
map.put("flightID", 2879);
final String body = HttpRequest.get("http://localhost:8884/api/test/testHttpUtilGetWithBody").body(JSONUtil.toJsonStr(map)).execute().bodyStr();
@SuppressWarnings("resource")
final String body = Request.of("http://localhost:8884/api/test/testHttpUtilGetWithBody").body(JSONUtil.toJsonStr(map)).send().bodyStr();
System.out.println("使用hutool返回结果:" + body);
}
}

View File

@@ -1,7 +1,8 @@
package cn.hutool.http;
import cn.hutool.core.lang.Console;
import cn.hutool.http.client.engine.jdk.HttpResponse;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.Response;
import cn.hutool.http.meta.Header;
import org.brotli.dec.BrotliInputStream;
import org.junit.Ignore;
@@ -14,9 +15,10 @@ public class IssueI5XBCFTest {
public void getTest() {
GlobalCompressStreamRegister.INSTANCE.register("br", BrotliInputStream.class);
@SuppressWarnings("resource") final HttpResponse s = HttpUtil.createGet("https://static-exp1.licdn.com/sc/h/br/1cp0oqz322bdprj3qd4pojqix")
@SuppressWarnings("resource")
final Response s = Request.of("https://static-exp1.licdn.com/sc/h/br/1cp0oqz322bdprj3qd4pojqix")
.header(Header.ACCEPT_ENCODING, "br")
.execute();
.send();
Console.log(s.body());
}
}

View File

@@ -1,8 +1,9 @@
package cn.hutool.http;
import cn.hutool.core.lang.Console;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.http.client.Request;
import cn.hutool.http.meta.Header;
import cn.hutool.http.meta.Method;
import cn.hutool.json.JSONUtil;
import org.junit.Assert;
import org.junit.Ignore;
@@ -18,21 +19,24 @@ public class RestTest {
@Test
public void contentTypeTest() {
final HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
final Request request = Request.of("http://localhost:8090/rest/restTest/")
.method(Method.POST)
.body(JSONUtil.ofObj()
.set("aaa", "aaaValue")
.set("键2", "值2").toString());
Assert.assertEquals("application/json;charset=UTF-8", request.header(Header.CONTENT_TYPE));
}
@SuppressWarnings("resource")
@Test
@Ignore
public void postTest() {
final HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
final Request request = Request.of("http://localhost:8090/rest/restTest/")
.method(Method.POST)
.body(JSONUtil.ofObj()
.set("aaa", "aaaValue")
.set("键2", "值2").toString());
Console.log(request.execute().body());
Console.log(request.send().body());
}
@Test
@@ -47,25 +51,12 @@ public class RestTest {
@Test
@Ignore
public void getWithBodyTest() {
final HttpRequest request = HttpRequest.get("http://localhost:8888/restTest")//
final Request request = Request.of("http://localhost:8888/restTest")//
.header(Header.CONTENT_TYPE, "application/json")
.body(JSONUtil.ofObj()
.set("aaa", "aaaValue")
.set("键2", "值2").toString());
Console.log(request.execute().body());
}
@Test
@Ignore
public void getWithBodyTest2() {
final HttpRequest request = HttpRequest.get("https://ad.oceanengine.com/open_api/2/advertiser/info/")//
// Charles代理
.setHttpProxy("localhost", 8888)
.header("Access-Token","")
.body(JSONUtil.ofObj()
.set("advertiser_ids", new Long[] {1690657248243790L})
.set("fields", new String[] {"id", "name", "status"}).toString());
Console.log(request);
Console.log(request.execute().body());
//noinspection resource
Console.log(request.send().body());
}
}

View File

@@ -1,16 +1,17 @@
package cn.hutool.http;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.MultiFileResource;
import cn.hutool.core.lang.Console;
import cn.hutool.http.client.engine.jdk.HttpRequest;
import cn.hutool.http.client.engine.jdk.HttpResponse;
import cn.hutool.core.map.MapUtil;
import cn.hutool.http.client.Request;
import cn.hutool.http.client.Response;
import cn.hutool.http.meta.Header;
import cn.hutool.http.meta.Method;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
@@ -30,12 +31,18 @@ public class UploadTest {
final File file = FileUtil.file("d:\\图片1.JPG");
final File file2 = FileUtil.file("d:\\图片3.png");
final Map<String, Object> form = MapUtil.builder(new HashMap<String, Object>())
.put("file", new MultiFileResource(file2, file))
.put("fileType", "图片")
.build();
// 方法一:自定义构建表单
final HttpRequest request = HttpRequest//
.post("http://localhost:8888/file")//
.form("file", file2, file)//
.form("fileType", "图片");
final HttpResponse response = request.execute();
final Request request = Request//
.of("http://localhost:8888/file")//
.method(Method.POST)
.form(form);
//noinspection resource
final Response response = request.send();
Console.log(response.body());
}
@@ -52,26 +59,6 @@ public class UploadTest {
System.out.println(result);
}
@Test
@Ignore
public void uploadTest2() {
//客户端
final String url = "http://192.168.1.200:8888/meta/upload/img";
final Path file = Paths.get("D:\\test\\testBigData_upload.xlsx");
final Map<String, String> headers = new HashMap<>(16);
headers.put("md5", "aaaaaaaa");
final Map<String, Object> params = new HashMap<>(16);
params.put("fileName", file.toFile().getName());
params.put("file", file.toFile());
final HttpRequest httpRequest = HttpRequest.post(url)
.setChunkedStreamingMode(1024 * 1024)
.headerMap(headers, false)
.form(params);
final HttpResponse httpResponse = httpRequest.execute();
Console.log(httpResponse);
}
@Test
@Ignore
public void smmsTest(){
@@ -79,11 +66,13 @@ public class UploadTest {
// hutool的user agent 被封了
final String token = "test";
final String url = "https://sm.ms/api/v2/upload";
final String result = HttpUtil.createPost(url)
//noinspection resource
final String result = Request.of(url)
.method(Method.POST)
.header(Header.USER_AGENT, "PostmanRuntime/7.28.4")
.auth(token)
.form("smfile", FileUtil.file("d:/test/qrcodeCustom.png"))
.execute().bodyStr();
.form(MapUtil.of("smfile", FileUtil.file("d:/test/qrcodeCustom.png")))
.send().bodyStr();
Console.log(result);
}