优化HttpRequest.toString()内容打印

This commit is contained in:
Looly 2023-04-19 18:33:08 +08:00
parent 66a8eabf8f
commit b0d528e27b
6 changed files with 45 additions and 11 deletions

View File

@ -2,9 +2,10 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.19.M1 (2023-04-18)
# 5.8.19.M1 (2023-04-19)
### 🐣新特性
* 【db 】 优化HttpRequest.toString()内容打印issue#3072@Github
### 🐞Bug修复
* 【core 】 修复URLUtil.decode无法解码UTF-16问题issue#3063@Github

View File

@ -347,14 +347,14 @@ public abstract class HttpBase<T> {
@Override
public String toString() {
final StringBuilder sb = StrUtil.builder();
sb.append("Request Headers: ").append(StrUtil.CRLF);
sb.append("Headers: ").append(StrUtil.CRLF);
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
sb.append(" ").append(
entry.getKey()).append(": ").append(CollUtil.join(entry.getValue(), ","))
.append(StrUtil.CRLF);
}
sb.append("Request Body: ").append(StrUtil.CRLF);
sb.append("Body: ").append(StrUtil.CRLF);
sb.append(" ").append(StrUtil.str(this.bodyBytes(), this.charset)).append(StrUtil.CRLF);
return sb.toString();

View File

@ -8,6 +8,7 @@ import cn.hutool.core.io.resource.FileResource;
import cn.hutool.core.io.resource.MultiFileResource;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.map.TableMap;
import cn.hutool.core.net.SSLUtil;
@ -29,6 +30,7 @@ import java.io.IOException;
import java.net.*;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
@ -1144,9 +1146,20 @@ public class HttpRequest extends HttpBase<HttpRequest> {
@Override
public String toString() {
StringBuilder sb = StrUtil.builder();
final StringBuilder sb = StrUtil.builder();
sb.append("Request Url: ").append(this.url.setCharset(this.charset)).append(StrUtil.CRLF);
sb.append(super.toString());
// header
sb.append("Request Headers: ").append(StrUtil.CRLF);
for (Map.Entry<String, List<String>> entry : this.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(createBody()).append(StrUtil.CRLF);
return sb.toString();
}
@ -1342,13 +1355,21 @@ public class HttpRequest extends HttpBase<HttpRequest> {
}
// Write的时候会优先使用body中的内容write时自动关闭OutputStream
RequestBody body;
createBody().writeClose(this.httpConnection.getOutputStream());
}
/**
* 创建body
*
* @return body
*/
private RequestBody createBody(){
// Write的时候会优先使用body中的内容write时自动关闭OutputStream
if (null != this.body) {
body = ResourceBody.create(this.body);
return ResourceBody.create(this.body);
} else {
body = FormUrlEncodedBody.create(this.form, this.charset);
return FormUrlEncodedBody.create(this.form, this.charset);
}
body.writeClose(this.httpConnection.getOutputStream());
}
/**

View File

@ -3,6 +3,7 @@ package cn.hutool.http.body;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.core.util.StrUtil;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.Map;
@ -35,4 +36,10 @@ public class FormUrlEncodedBody extends BytesBody {
super(StrUtil.bytes(UrlQuery.of(form, true).build(charset), charset));
}
@Override
public String toString() {
final ByteArrayOutputStream result = new ByteArrayOutputStream();
write(result);
return result.toString();
}
}

View File

@ -39,4 +39,9 @@ public class ResourceBody implements RequestBody {
this.resource.writeTo(out);
}
}
@Override
public String toString() {
return this.resource.readUtf8Str();
}
}

View File

@ -52,9 +52,9 @@ public class HttpRequestTest {
@Test
@Ignore
public void toStringTest() {
final String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
final String url = "https://hutool.cn?ccc=你好";
final HttpRequest request = HttpRequest.get(url).body("a=乌海");
final HttpRequest request = HttpRequest.get(url).form("a", "测试");
Console.log(request.toString());
}