mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add toString for HttpRequest
This commit is contained in:
parent
01ac0f94dc
commit
03827c7711
@ -11,6 +11,7 @@
|
||||
* 【core 】 Sftp类增加toString方法(issue#I1F2T4@Gitee)
|
||||
* 【core 】 修改FileUtil.size逻辑,不存在的文件返回0
|
||||
* 【extra 】 Sftp.ls遇到文件不存在返回空集合,而非抛异常(issue#844@Github)
|
||||
* 【http 】 改进HttpRequest.toString()格式,添加url
|
||||
|
||||
### Bug修复
|
||||
* 【db 】 修复PageResult.isLast计算问题
|
||||
|
@ -1,5 +1,11 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@ -8,11 +14,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* http基类
|
||||
* @author Looly
|
||||
@ -293,7 +294,9 @@ public abstract class HttpBase<T> {
|
||||
StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Request Headers: ").append(StrUtil.CRLF);
|
||||
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
|
||||
sb.append(" ").append(entry).append(StrUtil.CRLF);
|
||||
sb.append(" ").append(
|
||||
entry.getKey()).append(": ").append(CollUtil.join(entry.getValue(), ","))
|
||||
.append(StrUtil.CRLF);
|
||||
}
|
||||
|
||||
sb.append("Request Body: ").append(StrUtil.CRLF);
|
||||
|
@ -933,10 +933,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
public HttpResponse execute(boolean isAsync) {
|
||||
// 初始化URL
|
||||
urlWithParamIfGet();
|
||||
|
||||
// 初始化 connection
|
||||
initConnection();
|
||||
|
||||
// 发送请求
|
||||
send();
|
||||
|
||||
@ -975,6 +973,15 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
header(Header.AUTHORIZATION, content, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Request Url: ").append(this.url).append(StrUtil.CRLF);
|
||||
sb.append(super.toString());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
@ -1102,12 +1109,23 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
}
|
||||
|
||||
// Write的时候会优先使用body中的内容,write时自动关闭OutputStream
|
||||
if (ArrayUtil.isNotEmpty(this.bodyBytes)) {
|
||||
IoUtil.write(this.httpConnection.getOutputStream(), true, this.bodyBytes);
|
||||
} else {
|
||||
final String content = HttpUtil.toParams(this.form, this.charset);
|
||||
IoUtil.write(this.httpConnection.getOutputStream(), this.charset, true, content);
|
||||
byte[] content;
|
||||
if(ArrayUtil.isNotEmpty(this.bodyBytes)){
|
||||
content = this.bodyBytes;
|
||||
} else{
|
||||
content = StrUtil.bytes(getFormUrlEncoded(), this.charset);
|
||||
}
|
||||
IoUtil.write(this.httpConnection.getOutputStream(), true, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取编码后的表单数据,无表单数据返回""
|
||||
*
|
||||
* @return 编码后的表单数据,无表单数据返回""
|
||||
* @since 5.3.2
|
||||
*/
|
||||
private String getFormUrlEncoded() {
|
||||
return HttpUtil.toParams(this.form, this.charset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,15 +41,11 @@ public class HttpRequestTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithParamsTest() {
|
||||
public void toStringTest() {
|
||||
String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
|
||||
|
||||
HttpRequest request = HttpRequest.get(url).setEncodeUrlParams(true).body("a=乌海");
|
||||
String body = request.execute().body();
|
||||
Console.log(body);
|
||||
|
||||
// String body2 = HttpUtil.get(url);
|
||||
// Console.log(body2);
|
||||
HttpRequest request = HttpRequest.get(url).body("a=乌海");
|
||||
Console.log(request.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user