mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
HttpRequest#body增加支持Resource重载
This commit is contained in:
@@ -2,6 +2,7 @@ package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
@@ -53,7 +54,7 @@ public abstract class HttpBase<T> {
|
||||
/**
|
||||
* 存储主体
|
||||
*/
|
||||
protected byte[] bodyBytes;
|
||||
protected Resource body;
|
||||
|
||||
// ---------------------------------------------------------------- Headers start
|
||||
|
||||
@@ -303,7 +304,7 @@ public abstract class HttpBase<T> {
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] bodyBytes() {
|
||||
return this.bodyBytes;
|
||||
return this.body.readBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -345,7 +346,7 @@ public abstract class HttpBase<T> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = StrUtil.builder();
|
||||
final StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Request Headers: ").append(StrUtil.CRLF);
|
||||
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
|
||||
sb.append(" ").append(
|
||||
@@ -354,7 +355,7 @@ public abstract class HttpBase<T> {
|
||||
}
|
||||
|
||||
sb.append("Request Body: ").append(StrUtil.CRLF);
|
||||
sb.append(" ").append(StrUtil.str(this.bodyBytes, this.charset)).append(StrUtil.CRLF);
|
||||
sb.append(" ").append(StrUtil.str(this.body.readBytes(), this.charset)).append(StrUtil.CRLF);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
@@ -16,10 +16,10 @@ import cn.hutool.core.net.url.UrlQuery;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.body.BytesBody;
|
||||
import cn.hutool.http.body.FormUrlEncodedBody;
|
||||
import cn.hutool.http.body.MultipartBody;
|
||||
import cn.hutool.http.body.RequestBody;
|
||||
import cn.hutool.http.body.ResourceBody;
|
||||
import cn.hutool.http.cookie.GlobalCookieManager;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
@@ -499,7 +499,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
}
|
||||
|
||||
// 停用body
|
||||
this.bodyBytes = null;
|
||||
this.body = null;
|
||||
|
||||
if (value instanceof File) {
|
||||
// 文件上传
|
||||
@@ -752,8 +752,22 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
* @return this
|
||||
*/
|
||||
public HttpRequest body(byte[] bodyBytes) {
|
||||
if (null != bodyBytes) {
|
||||
this.bodyBytes = bodyBytes;
|
||||
if (ArrayUtil.isNotEmpty(bodyBytes)) {
|
||||
return body(new BytesResource(bodyBytes));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置主体字节码<br>
|
||||
* 需在此方法调用前使用charset方法设置编码,否则使用默认编码UTF-8
|
||||
*
|
||||
* @param resource 主体
|
||||
* @return this
|
||||
*/
|
||||
public HttpRequest body(Resource resource) {
|
||||
if (null != resource) {
|
||||
this.body = resource;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -1225,8 +1239,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
}
|
||||
|
||||
// 优先使用body形式的参数,不存在使用form
|
||||
if (ArrayUtil.isNotEmpty(this.bodyBytes)) {
|
||||
query.parse(StrUtil.str(this.bodyBytes, this.charset), this.charset);
|
||||
if (null != this.body) {
|
||||
query.parse(StrUtil.str(this.body.readBytes(), this.charset), this.charset);
|
||||
} else {
|
||||
query.addAll(this.form);
|
||||
}
|
||||
@@ -1319,8 +1333,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
|
||||
// Write的时候会优先使用body中的内容,write时自动关闭OutputStream
|
||||
RequestBody body;
|
||||
if (ArrayUtil.isNotEmpty(this.bodyBytes)) {
|
||||
body = BytesBody.create(this.bodyBytes);
|
||||
if (null != this.body) {
|
||||
body = ResourceBody.create(this.body);
|
||||
} else {
|
||||
body = FormUrlEncodedBody.create(this.form, this.charset);
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.StreamProgress;
|
||||
import cn.hutool.core.io.resource.BytesResource;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
@@ -13,7 +14,6 @@ import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.cookie.GlobalCookieManager;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
@@ -222,7 +222,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
||||
* @since 4.1.4
|
||||
*/
|
||||
public String getCookieValue(String name) {
|
||||
HttpCookie cookie = getCookie(name);
|
||||
final HttpCookie cookie = getCookie(name);
|
||||
return (null == cookie) ? null : cookie.getValue();
|
||||
}
|
||||
// ---------------------------------------------------------------- Http Response Header end
|
||||
@@ -241,7 +241,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
||||
if (isAsync) {
|
||||
return this.in;
|
||||
}
|
||||
return new ByteArrayInputStream(this.bodyBytes);
|
||||
return this.body.getStream();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +253,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
||||
@Override
|
||||
public byte[] bodyBytes() {
|
||||
sync();
|
||||
return this.bodyBytes;
|
||||
return this.body.readBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,7 +266,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
||||
public HttpResponse body(byte[] bodyBytes) {
|
||||
sync();
|
||||
if (null != bodyBytes) {
|
||||
this.bodyBytes = bodyBytes;
|
||||
this.body = new BytesResource(bodyBytes);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -606,7 +606,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
||||
final long contentLength = contentLength();
|
||||
final FastByteArrayOutputStream out = new FastByteArrayOutputStream((int) contentLength);
|
||||
copyBody(in, out, contentLength, null, this.config.ignoreEOFError);
|
||||
this.bodyBytes = out.toByteArray();
|
||||
this.body = new BytesResource(out.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,42 @@
|
||||
package cn.hutool.http.body;
|
||||
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* {@link Resource}类型的Http request body,主要发送编码后的表单数据或rest body(如JSON或XML)
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.8.13
|
||||
*/
|
||||
public class ResourceBody implements RequestBody {
|
||||
|
||||
private final Resource resource;
|
||||
|
||||
/**
|
||||
* 创建 Http request body
|
||||
*
|
||||
* @param resource body内容,编码后
|
||||
* @return BytesBody
|
||||
*/
|
||||
public static ResourceBody create(Resource resource) {
|
||||
return new ResourceBody(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param resource Body内容,编码后
|
||||
*/
|
||||
public ResourceBody(Resource resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(OutputStream out) {
|
||||
if(null != this.resource){
|
||||
this.resource.writeTo(out);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.io.resource.StringResource;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Assert;
|
||||
@@ -10,7 +11,6 @@ import org.junit.Test;
|
||||
* Rest类型请求单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class RestTest {
|
||||
|
||||
@@ -26,10 +26,10 @@ public class RestTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj()
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8888/restTest/")//
|
||||
.body(new StringResource(JSONUtil.createObj()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
.set("键2", "值2").toString()));
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@@ -59,17 +59,17 @@ public class RestTest {
|
||||
HttpRequest request = HttpRequest.get("https://ad.oceanengine.com/open_api/2/advertiser/info/")//
|
||||
// Charles代理
|
||||
.setHttpProxy("localhost", 8888)
|
||||
.header("Access-Token","")
|
||||
.header("Access-Token", "")
|
||||
.body(JSONUtil.createObj()
|
||||
.set("advertiser_ids", new Long[] {1690657248243790L})
|
||||
.set("fields", new String[] {"id", "name", "status"}).toString());
|
||||
.set("advertiser_ids", new Long[]{1690657248243790L})
|
||||
.set("fields", new String[]{"id", "name", "status"}).toString());
|
||||
Console.log(request);
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest(){
|
||||
public void getTest() {
|
||||
final HttpRequest request = HttpRequest.get("http://localhost:8888/restTest");
|
||||
final HttpResponse execute = request.execute();
|
||||
Console.log(execute.headers());
|
||||
|
Reference in New Issue
Block a user