mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!1245 HttpUtil添加Request的静态toString方法,Request添加bodyStr、bodyBytes、bodyStream、toString方法
Merge pull request !1245 from Toint/v6-dev
This commit is contained in:
commit
ef8b2fdeaa
@ -315,6 +315,32 @@ public class HttpUtil {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印 {@link Request} 为可读形式
|
||||
*
|
||||
* @param request {@link Request}
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String toString(final Request request) {
|
||||
final StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Request Url: ").append(request.url()).append(StrUtil.CRLF);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// body
|
||||
sb.append("Request Body: ").append(StrUtil.CRLF);
|
||||
sb.append(" ").append(request.bodyStr()).append(StrUtil.CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的Header值,如果不存在返回{@code null}<br>
|
||||
* 根据RFC2616规范,header的name不区分大小写,因此首先get值,不存在则遍历匹配不区分大小写的key。
|
||||
|
@ -13,6 +13,7 @@
|
||||
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;
|
||||
@ -23,6 +24,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.http.GlobalHeaders;
|
||||
import org.dromara.hutool.http.HttpGlobalConfig;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.body.*;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
|
||||
@ -278,6 +280,44 @@ public class Request implements HeaderOperation<Request> {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求体字符串
|
||||
*
|
||||
* @return 请求体字符串
|
||||
*/
|
||||
public String bodyStr() {
|
||||
InputStream bodyStream = this.bodyStream();
|
||||
if (bodyStream == null) {
|
||||
return null;
|
||||
}
|
||||
return IoUtil.read(bodyStream, this.charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求体字节码
|
||||
*
|
||||
* @return 请求体字节码
|
||||
*/
|
||||
public byte[] bodyBytes() {
|
||||
InputStream bodyStream = this.bodyStream();
|
||||
if (bodyStream == null) {
|
||||
return null;
|
||||
}
|
||||
return IoUtil.readBytes(bodyStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求体资源流
|
||||
*
|
||||
* @return 请求体资源流
|
||||
*/
|
||||
public InputStream bodyStream() {
|
||||
if (this.body == null) {
|
||||
return null;
|
||||
}
|
||||
return this.body.getStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取处理过的请求体,即如果是非REST的GET请求,始终返回{@code null}
|
||||
*
|
||||
@ -415,4 +455,9 @@ public class Request implements HeaderOperation<Request> {
|
||||
|
||||
return this.url();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpUtil.toString(this);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user