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