This commit is contained in:
Looly 2022-10-27 03:30:28 +08:00
parent 35a8f5e281
commit f189c5e027
9 changed files with 37 additions and 35 deletions

View File

@ -279,9 +279,9 @@ public class HttpUtil {
*
* @param url 请求的url
* @param dest 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @return 文件大小
* @return 文件
*/
public static long downloadFile(final String url, final String dest) {
public static File downloadFile(final String url, final String dest) {
return downloadFile(url, FileUtil.file(dest));
}
@ -290,9 +290,9 @@ public class HttpUtil {
*
* @param url 请求的url
* @param destFile 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @return 文件大小
* @return 文件
*/
public static long downloadFile(final String url, final File destFile) {
public static File downloadFile(final String url, final File destFile) {
return downloadFile(url, destFile, null);
}
@ -302,10 +302,10 @@ public class HttpUtil {
* @param url 请求的url
* @param destFile 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @param timeout 超时单位毫秒-1表示默认超时
* @return 文件大小
* @return 文件
* @since 4.0.4
*/
public static long downloadFile(final String url, final File destFile, final int timeout) {
public static File downloadFile(final String url, final File destFile, final int timeout) {
return downloadFile(url, destFile, timeout, null);
}
@ -315,9 +315,9 @@ public class HttpUtil {
* @param url 请求的url
* @param destFile 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @param streamProgress 进度条
* @return 文件大小
* @return 文件
*/
public static long downloadFile(final String url, final File destFile, final StreamProgress streamProgress) {
public static File downloadFile(final String url, final File destFile, final StreamProgress streamProgress) {
return downloadFile(url, destFile, -1, streamProgress);
}
@ -328,10 +328,10 @@ public class HttpUtil {
* @param destFile 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @param timeout 超时单位毫秒-1表示默认超时
* @param streamProgress 进度条
* @return 文件大小
* @return 文件
* @since 4.0.4
*/
public static long downloadFile(final String url, final File destFile, final int timeout, final StreamProgress streamProgress) {
public static File downloadFile(final String url, final File destFile, final int timeout, final StreamProgress streamProgress) {
return HttpDownloader.downloadFile(url, destFile, timeout, streamProgress);
}
@ -473,8 +473,8 @@ public class HttpUtil {
* key1=v1&key2=&key3=v3
* </pre>
*
* @param paramMap 表单数据
* @param charset 编码null表示不encode键值对
* @param paramMap 表单数据
* @param charset 编码null表示不encode键值对
* @param isFormUrlEncoded 是否为x-www-form-urlencoded模式此模式下空格会编码为'+'
* @return url参数
* @since 5.7.16
@ -534,7 +534,7 @@ public class HttpUtil {
* @since 4.5.2
*/
public static String normalizeParams(final String paramPart, final Charset charset) {
if(StrUtil.isEmpty(paramPart)){
if (StrUtil.isEmpty(paramPart)) {
return paramPart;
}
final StringBuilder builder = new StringBuilder(paramPart.length() + 16);

View File

@ -17,6 +17,7 @@ import java.nio.charset.Charset;
* @author looly
* @since 5.6.4
*/
@SuppressWarnings("resource")
public class HttpDownloader {
/**
@ -50,10 +51,10 @@ public class HttpDownloader {
* @param targetFileOrDir 目标文件或目录当为目录时取URL中的文件名取不到使用编码后的URL做为文件名
* @param timeout 超时单位毫秒-1表示默认超时
* @param streamProgress 进度条
* @return 文件大小
* @return 文件
*/
public static long downloadFile(final String url, final File targetFileOrDir, final int timeout, final StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBody(targetFileOrDir, streamProgress);
public static File downloadFile(final String url, final File targetFileOrDir, final int timeout, final StreamProgress streamProgress) {
return requestDownload(url, timeout).body().write(targetFileOrDir, streamProgress);
}
/**
@ -66,11 +67,11 @@ public class HttpDownloader {
* @param tempFileSuffix 临时文件后缀默认".temp"
* @param timeout 超时单位毫秒-1表示默认超时
* @param streamProgress 进度条
* @return 下载大小
* @return 文件
* @since 5.7.12
*/
public long downloadFile(final String url, final File targetFileOrDir, final String tempFileSuffix, final int timeout, final StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBody(targetFileOrDir, tempFileSuffix, streamProgress);
public File downloadFile(final String url, final File targetFileOrDir, final String tempFileSuffix, final int timeout, final StreamProgress streamProgress) {
return requestDownload(url, timeout).body().write(targetFileOrDir, tempFileSuffix, streamProgress);
}
/**
@ -83,7 +84,7 @@ public class HttpDownloader {
* @return 文件
*/
public static File downloadForFile(final String url, final File targetFileOrDir, final int timeout, final StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBodyForFile(targetFileOrDir, streamProgress);
return requestDownload(url, timeout).body().write(targetFileOrDir, streamProgress);
}
/**
@ -98,7 +99,7 @@ public class HttpDownloader {
public static long download(final String url, final OutputStream out, final boolean isCloseOut, final StreamProgress streamProgress) {
Assert.notNull(out, "[out] is null !");
return requestDownload(url, -1).writeBody(out, isCloseOut, streamProgress);
return requestDownload(url, -1).body().write(out, isCloseOut, streamProgress);
}
/**

View File

@ -1,6 +1,7 @@
package cn.hutool.http.client.body;
import cn.hutool.core.io.resource.BytesResource;
import cn.hutool.core.io.resource.HttpResource;
/**
* bytes类型的Http request body主要发送编码后的表单数据或rest body如JSON或XML
@ -25,6 +26,6 @@ public class BytesBody extends ResourceBody {
* @param content Body内容编码后
*/
public BytesBody(final byte[] content) {
super(new BytesResource(content));
super(new HttpResource(new BytesResource(content), null));
}
}

View File

@ -574,7 +574,7 @@ public class SoapClient extends HttpBase<SoapClient> {
* @return 返回结果
*/
public String send(final boolean pretty) {
final String body = sendForResponse().body();
final String body = sendForResponse().bodyStr();
return pretty ? XmlUtil.format(body) : body;
}

View File

@ -34,13 +34,13 @@ 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().writeBody("e:/pic/366466.jpg");
HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().body().write("e:/pic/366466.jpg");
}
@Test
@Ignore
public void downloadTest1() {
final long size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/");
final File size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/");
System.out.println("Download size: " + size);
}

View File

@ -31,7 +31,7 @@ public class HttpRequestTest {
@Test
@Ignore
public void getHttpsTest() {
final String body = HttpRequest.get("https://www.hutool.cn/").timeout(10).execute().body();
final String body = HttpRequest.get("https://www.hutool.cn/").timeout(10).execute().bodyStr();
Console.log(body);
}
@ -48,7 +48,7 @@ public class HttpRequestTest {
public void getCookiesTest() {
// 检查在Connection关闭情况下Cookie是否可以正常获取
final HttpResponse res = HttpRequest.get("https://www.oschina.net/").execute();
final String body = res.body();
final String body = res.bodyStr();
Console.log(res.getCookies());
Console.log(body);
}
@ -129,7 +129,7 @@ public class HttpRequestTest {
@Test
@Ignore
public void bodyTest() {
final String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().body();
final String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().bodyStr();
Console.log(ddddd1);
}

View File

@ -40,7 +40,7 @@ public class HttpUtilTest {
public void postTest() {
final String result = HttpUtil.createPost("api.uhaozu.com/goods/description/1120448506")
.charset(CharsetUtil.NAME_UTF_8)
.execute().body();
.execute().bodyStr();
Console.log(result);
}
@ -52,7 +52,7 @@ public class HttpUtilTest {
.createPost("http://cmp.ishanghome.com/cmp/v1/community/queryClusterCommunity")
.header(Header.ACCEPT, "*/*")
.execute()
.body();
.bodyStr();
Console.log(result);
}
@ -70,7 +70,7 @@ public class HttpUtilTest {
// 自定义的默认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().body();
.removeHeader(Header.USER_AGENT).execute().bodyStr();
Console.log(result);
}
@ -272,7 +272,7 @@ public class HttpUtilTest {
@Ignore
public void patchTest() {
// 验证patch请求是否可用
final String body = HttpRequest.patch("https://www.baidu.com").execute().body();
final String body = HttpRequest.patch("https://www.baidu.com").execute().bodyStr();
Console.log(body);
}
@ -368,7 +368,7 @@ public class HttpUtilTest {
@Ignore
public void acplayTest(){
final String body = HttpRequest.get("https://api.acplay.net/api/v2/bangumi/9541")
.execute().body();
.execute().bodyStr();
Console.log(body);
}

View File

@ -19,7 +19,7 @@ public class IssueI5WAV4Test {
map.put("flightID", 2879);
final String body = HttpRequest.get("http://localhost:8884/api/test/testHttpUtilGetWithBody").body(JSONUtil.toJsonStr(map)).execute().body();
final String body = HttpRequest.get("http://localhost:8884/api/test/testHttpUtilGetWithBody").body(JSONUtil.toJsonStr(map)).execute().bodyStr();
System.out.println("使用hutool返回结果:" + body);
}
}

View File

@ -83,7 +83,7 @@ public class UploadTest {
.header(Header.USER_AGENT, "PostmanRuntime/7.28.4")
.auth(token)
.form("smfile", FileUtil.file("d:/test/qrcodeCustom.png"))
.execute().body();
.execute().bodyStr();
Console.log(result);
}