mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
HttpUtil添加Request的静态toString方法,Request添加bodyStr、bodyBytes、bodyStream、toString方法
This commit is contained in:
parent
ef8b2fdeaa
commit
3dd473b2c4
@ -27,7 +27,6 @@ import org.dromara.hutool.http.server.SimpleServer;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -304,11 +303,12 @@ public class HttpUtil {
|
||||
public static String toString(final Response response) {
|
||||
final StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Response Status: ").append(response.getStatus()).append(StrUtil.CRLF);
|
||||
sb.append("Response Headers: ").append(StrUtil.CRLF);
|
||||
for (final Map.Entry<String, List<String>> entry : response.headers().entrySet()) {
|
||||
sb.append(" ").append(entry).append(StrUtil.CRLF);
|
||||
}
|
||||
|
||||
// header
|
||||
sb.append("Response Headers: ").append(StrUtil.CRLF);
|
||||
response.headers().forEach((key, value) -> sb.append(" ").append(key).append(": ").append(CollUtil.join(value, ",")).append(StrUtil.CRLF));
|
||||
|
||||
// body
|
||||
sb.append("Response Body: ").append(StrUtil.CRLF);
|
||||
sb.append(" ").append(response.bodyStr()).append(StrUtil.CRLF);
|
||||
|
||||
@ -327,13 +327,7 @@ public class HttpUtil {
|
||||
|
||||
// header
|
||||
sb.append("Request Headers: ").append(StrUtil.CRLF);
|
||||
for (Map.Entry<String, ? extends Collection<String>> entry : request.headers().entrySet()) {
|
||||
sb.append(" ")
|
||||
.append(entry.getKey())
|
||||
.append(": ")
|
||||
.append(CollUtil.join(entry.getValue(), ","))
|
||||
.append(StrUtil.CRLF);
|
||||
}
|
||||
request.headers().forEach((key, value) -> sb.append(" ").append(key).append(": ").append(CollUtil.join(value, ",")).append(StrUtil.CRLF));
|
||||
|
||||
// body
|
||||
sb.append("Request Body: ").append(StrUtil.CRLF);
|
||||
|
@ -13,7 +13,6 @@
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.resource.Resource;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
@ -271,6 +270,7 @@ public class Request implements HeaderOperation<Request> {
|
||||
return this;
|
||||
}
|
||||
|
||||
// region body get
|
||||
/**
|
||||
* 获取请求体
|
||||
*
|
||||
@ -286,11 +286,7 @@ public class Request implements HeaderOperation<Request> {
|
||||
* @return 请求体字符串
|
||||
*/
|
||||
public String bodyStr() {
|
||||
InputStream bodyStream = this.bodyStream();
|
||||
if (bodyStream == null) {
|
||||
return null;
|
||||
}
|
||||
return IoUtil.read(bodyStream, this.charset);
|
||||
return StrUtil.str(bodyBytes(), this.charset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -299,11 +295,7 @@ public class Request implements HeaderOperation<Request> {
|
||||
* @return 请求体字节码
|
||||
*/
|
||||
public byte[] bodyBytes() {
|
||||
InputStream bodyStream = this.bodyStream();
|
||||
if (bodyStream == null) {
|
||||
return null;
|
||||
}
|
||||
return IoUtil.readBytes(bodyStream);
|
||||
return this.body == null ? null : this.body.getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -312,10 +304,7 @@ public class Request implements HeaderOperation<Request> {
|
||||
* @return 请求体资源流
|
||||
*/
|
||||
public InputStream bodyStream() {
|
||||
if (this.body == null) {
|
||||
return null;
|
||||
}
|
||||
return this.body.getStream();
|
||||
return this.body == null ? null : this.body.getStream();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -329,7 +318,9 @@ public class Request implements HeaderOperation<Request> {
|
||||
}
|
||||
return body();
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region body set
|
||||
/**
|
||||
* 添加请求表单内容
|
||||
*
|
||||
@ -380,6 +371,7 @@ public class Request implements HeaderOperation<Request> {
|
||||
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 获取最大重定向请求次数
|
||||
|
@ -93,4 +93,17 @@ public interface HttpBody {
|
||||
writeClose(out);
|
||||
return IoUtil.toStream(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求(响应)体字节码
|
||||
*
|
||||
* @return 请求体字节码
|
||||
*/
|
||||
default byte[] getBytes() {
|
||||
final InputStream bodyStream = getStream();
|
||||
if (bodyStream == null) {
|
||||
return null;
|
||||
}
|
||||
return IoUtil.readBytes(bodyStream);
|
||||
}
|
||||
}
|
||||
|
@ -83,6 +83,7 @@ public class ResponseBody implements HttpBody, Closeable {
|
||||
*
|
||||
* @return 响应内容bytes
|
||||
*/
|
||||
@Override
|
||||
public byte[] getBytes() {
|
||||
return this.bodyStream.readBytes();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user