mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
package cn.hutool.http.client;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.http.HttpException;
|
||||
import cn.hutool.http.client.body.ResponseBody;
|
||||
import cn.hutool.http.html.HtmlUtil;
|
||||
import cn.hutool.http.meta.ContentTypeUtil;
|
||||
import cn.hutool.http.meta.Header;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
@@ -79,17 +80,25 @@ public interface Response extends Closeable {
|
||||
* @throws HttpException 包装IO异常
|
||||
*/
|
||||
default String bodyStr() throws HttpException {
|
||||
return HtmlUtil.getString(bodyBytes(), charset(), true);
|
||||
try(final ResponseBody body = body()){
|
||||
return body.getString();
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取响应流字节码<br>
|
||||
* 此方法会转为同步模式
|
||||
* 此方法会转为同步模式,读取响应流并关闭之
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
default byte[] bodyBytes() {
|
||||
return body().getBytes();
|
||||
try(final ResponseBody body = body()){
|
||||
return body.getBytes();
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -11,6 +11,7 @@ import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.http.HttpException;
|
||||
import cn.hutool.http.client.Response;
|
||||
import cn.hutool.http.html.HtmlUtil;
|
||||
import cn.hutool.http.meta.Header;
|
||||
|
||||
import java.io.Closeable;
|
||||
@@ -79,6 +80,15 @@ public class ResponseBody implements HttpBody, Closeable {
|
||||
return this.bodyStream.readBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取响应字符串,自动识别判断编码
|
||||
*
|
||||
* @return 响应字符串
|
||||
*/
|
||||
public String getString() {
|
||||
return HtmlUtil.getString(getBytes(), response.charset(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将响应内容写出到{@link OutputStream}<br>
|
||||
* 异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出<br>
|
||||
@@ -189,6 +199,11 @@ public class ResponseBody implements HttpBody, Closeable {
|
||||
this.bodyStream.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getString();
|
||||
}
|
||||
|
||||
// region ---------------------------------------------------------------------------- Private Methods
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user