mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
优化HttpRequest.toString()内容打印
This commit is contained in:
parent
66a8eabf8f
commit
b0d528e27b
@ -2,9 +2,10 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.19.M1 (2023-04-18)
|
# 5.8.19.M1 (2023-04-19)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【db 】 优化HttpRequest.toString()内容打印(issue#3072@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复URLUtil.decode无法解码UTF-16问题(issue#3063@Github)
|
* 【core 】 修复URLUtil.decode无法解码UTF-16问题(issue#3063@Github)
|
||||||
|
@ -347,14 +347,14 @@ public abstract class HttpBase<T> {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder sb = StrUtil.builder();
|
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()) {
|
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
|
||||||
sb.append(" ").append(
|
sb.append(" ").append(
|
||||||
entry.getKey()).append(": ").append(CollUtil.join(entry.getValue(), ","))
|
entry.getKey()).append(": ").append(CollUtil.join(entry.getValue(), ","))
|
||||||
.append(StrUtil.CRLF);
|
.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);
|
sb.append(" ").append(StrUtil.str(this.bodyBytes(), this.charset)).append(StrUtil.CRLF);
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
@ -8,6 +8,7 @@ import cn.hutool.core.io.resource.FileResource;
|
|||||||
import cn.hutool.core.io.resource.MultiFileResource;
|
import cn.hutool.core.io.resource.MultiFileResource;
|
||||||
import cn.hutool.core.io.resource.Resource;
|
import cn.hutool.core.io.resource.Resource;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.map.TableMap;
|
import cn.hutool.core.map.TableMap;
|
||||||
import cn.hutool.core.net.SSLUtil;
|
import cn.hutool.core.net.SSLUtil;
|
||||||
@ -29,6 +30,7 @@ import java.io.IOException;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@ -1144,9 +1146,20 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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("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();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1342,13 +1355,21 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write的时候会优先使用body中的内容,write时自动关闭OutputStream
|
// 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) {
|
if (null != this.body) {
|
||||||
body = ResourceBody.create(this.body);
|
return ResourceBody.create(this.body);
|
||||||
} else {
|
} else {
|
||||||
body = FormUrlEncodedBody.create(this.form, this.charset);
|
return FormUrlEncodedBody.create(this.form, this.charset);
|
||||||
}
|
}
|
||||||
body.writeClose(this.httpConnection.getOutputStream());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.http.body;
|
|||||||
import cn.hutool.core.net.url.UrlQuery;
|
import cn.hutool.core.net.url.UrlQuery;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -35,4 +36,10 @@ public class FormUrlEncodedBody extends BytesBody {
|
|||||||
super(StrUtil.bytes(UrlQuery.of(form, true).build(charset), charset));
|
super(StrUtil.bytes(UrlQuery.of(form, true).build(charset), charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||||
|
write(result);
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,4 +39,9 @@ public class ResourceBody implements RequestBody {
|
|||||||
this.resource.writeTo(out);
|
this.resource.writeTo(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.resource.readUtf8Str();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,9 +52,9 @@ public class HttpRequestTest {
|
|||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void toStringTest() {
|
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());
|
Console.log(request.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user