mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
add toString
This commit is contained in:
parent
1f9e3f9f9f
commit
c581a8ecca
@ -23,6 +23,7 @@ import org.dromara.hutool.http.meta.Method;
|
||||
import org.dromara.hutool.http.server.SimpleServer;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -234,4 +235,23 @@ public class HttpUtil {
|
||||
public static SimpleServer createServer(final int port) {
|
||||
return new SimpleServer(port);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印{@link Response} 为可读形式
|
||||
* @param response {@link Response}
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String toString(final Response response) {
|
||||
final StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Response Status: ").append(response.getStatus()).append(StrUtil.CRLF);
|
||||
sb.append("Response Headers: ").append(StrUtil.CRLF);
|
||||
for (final Map.Entry<String, List<String>> entry : response.headers().entrySet()) {
|
||||
sb.append(" ").append(entry).append(StrUtil.CRLF);
|
||||
}
|
||||
|
||||
sb.append("Response Body: ").append(StrUtil.CRLF);
|
||||
sb.append(" ").append(response.bodyStr()).append(StrUtil.CRLF);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.HttpException;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.ParseException;
|
||||
@ -121,4 +122,10 @@ public class HttpClient4Response implements Response {
|
||||
public void close() throws IOException {
|
||||
rawRes.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpUtil.toString(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.HttpException;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.ClassicHttpResponse;
|
||||
@ -122,4 +123,9 @@ public class HttpClient5Response implements Response {
|
||||
public void close() throws IOException {
|
||||
rawRes.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpUtil.toString(this);
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.HttpException;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.body.ResponseBody;
|
||||
import org.dromara.hutool.http.client.cookie.GlobalCookieManager;
|
||||
@ -222,16 +223,7 @@ public class JdkHttpResponse implements Response, Closeable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = StrUtil.builder();
|
||||
sb.append("Response Headers: ").append(StrUtil.CRLF);
|
||||
for (final Entry<String, List<String>> entry : this.headers.entrySet()) {
|
||||
sb.append(" ").append(entry).append(StrUtil.CRLF);
|
||||
}
|
||||
|
||||
sb.append("Response Body: ").append(StrUtil.CRLF);
|
||||
sb.append(" ").append(this.bodyStr()).append(StrUtil.CRLF);
|
||||
|
||||
return sb.toString();
|
||||
return HttpUtil.toString(this);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Private method start
|
||||
|
@ -18,6 +18,7 @@ import okhttp3.ResponseBody;
|
||||
import org.dromara.hutool.core.io.stream.EmptyInputStream;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.GlobalCompressStreamRegister;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
|
||||
@ -90,4 +91,9 @@ public class OkHttpResponse implements Response {
|
||||
rawRes.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return HttpUtil.toString(this);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2023. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.http.client.engine.jdk.JdkClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.okhttp.OkHttpEngine;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HttpClientSendTest {
|
||||
@Test
|
||||
void sendTest() {
|
||||
final String json = JSONUtil.ofObj()
|
||||
.set("contract_id", "1387572813382368")
|
||||
.set("txt_url", "http://47.99.192.138:9000/oss/20230915/test.txt")
|
||||
.set("docx_url", "http://47.99.192.138:9000/oss/20230915/test.docx")
|
||||
.toString();
|
||||
|
||||
final Response response =
|
||||
Request.of("http://47.99.192.138:8012/extract/counterparty/")
|
||||
.method(Method.POST)
|
||||
.auth(
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbklkIjoidGVzdGxvZ2luMSIsImV4cCI6MTU1NjcxMjAzNn0.1ukpIm0xAqXFAvHk7U8eFJbhpiYTV8VtdaiBl_JqQ0s")
|
||||
.body(json)
|
||||
.send(new OkHttpEngine());
|
||||
|
||||
Console.log(response);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user