add config

This commit is contained in:
Looly 2023-05-17 23:15:25 +08:00
parent 186d58396e
commit 87fd7393f9
3 changed files with 28 additions and 2 deletions

View File

@ -41,6 +41,10 @@ public class HttpGlobalConfig implements Serializable {
private static int maxRedirectCount = 0;
private static boolean ignoreEOFError = true;
private static boolean decodeUrl = false;
/**
* 是否从响应正文中的meta标签获取编码信息
*/
private static boolean getCharsetFromContent = true;
/**
* 获取全局默认的超时时长
@ -187,4 +191,24 @@ public class HttpGlobalConfig implements Serializable {
synchronized public static void closeCookie() {
GlobalCookieManager.setCookieManager(null);
}
/**
* 设置是否从响应正文中的meta标签获取编码信息
*
* @param customGetCharsetFromContent 是否从响应正文中的meta标签获取编码信息
* @since 6.0.0
*/
synchronized public static void setGetCharsetFromContent(final boolean customGetCharsetFromContent){
getCharsetFromContent = customGetCharsetFromContent;
}
/**
* 是否从响应正文中的meta标签获取编码信息
*
* @return 是否从响应正文中的meta标签获取编码信息
* @since 6.0.0
*/
public static boolean isGetCharsetFromContent(){
return getCharsetFromContent;
}
}

View File

@ -22,6 +22,7 @@ import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpGlobalConfig;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.html.HtmlUtil;
import org.dromara.hutool.http.meta.HeaderName;
@ -98,7 +99,7 @@ public class ResponseBody implements HttpBody, Closeable {
* @return 响应字符串
*/
public String getString() {
return HtmlUtil.getString(getBytes(), response.charset(), true);
return HtmlUtil.getString(getBytes(), response.charset(), HttpGlobalConfig.isGetCharsetFromContent());
}
/**

View File

@ -76,7 +76,8 @@ public class JdkHttpResponse implements Response, Closeable {
* @param isAsync 是否异步
* @param isIgnoreBody 是否忽略读取响应体
*/
protected JdkHttpResponse(final JdkHttpConnection httpConnection, final boolean ignoreEOFError, final Charset requestCharset, final boolean isAsync, final boolean isIgnoreBody) {
protected JdkHttpResponse(final JdkHttpConnection httpConnection, final boolean ignoreEOFError,
final Charset requestCharset, final boolean isAsync, final boolean isIgnoreBody) {
this.httpConnection = httpConnection;
this.ignoreEOFError = ignoreEOFError;
this.requestCharset = requestCharset;