HttpConfig增加参数setIgnoreContentLength可选忽略读取响应contentLength头(issue#ICB1B8@Gitee)

This commit is contained in:
Looly 2025-06-09 12:01:29 +08:00
parent fe597605cf
commit 2c8070f324
3 changed files with 20 additions and 1 deletions

View File

@ -8,6 +8,7 @@
* 【ai 】 增加SSE流式返回函数参数callback豆包、grok新增文生图接口豆包生成视频支持使用model
* 【core 】 DesensitizedUtil新增护照号码脱敏功能pr#1347@Gitee
* 【core 】 优化XXXToMapCopier的部分性能pr#1345@Gitee
* 【http 】 `HttpConfig`增加参数`setIgnoreContentLength`可选忽略读取响应contentLength头issue#ICB1B8@Gitee
*
### 🐞Bug修复
* 【core 】 修复`NumberUtil`isNumber方法以L结尾没有小数点判断问题issue#3938@Github

View File

@ -101,6 +101,12 @@ public class HttpConfig {
*/
boolean useDefaultContentTypeIfNull = true;
/**
* 是否忽略Content-Length如果为true则忽略Content-Length自动根据响应内容计算Content-Length<br>
* issue#ICB1B8@Gitee此参数主要解决服务端响应中Content-Length错误的问题
*/
boolean ignoreContentLength = false;
/**
* 设置超时单位毫秒<br>
* 超时包括
@ -332,4 +338,15 @@ public class HttpConfig {
this.useDefaultContentTypeIfNull = useDefaultContentTypeIfNull;
return this;
}
/**
* 设置是否忽略Content-Length如果为true则忽略Content-Length自动根据响应内容计算Content-Length
* @param ignoreContentLength 是否忽略Content-Length
* @return this
* @since 5.8.39
*/
public HttpConfig setIgnoreContentLength(boolean ignoreContentLength) {
this.ignoreContentLength = ignoreContentLength;
return this;
}
}

View File

@ -664,7 +664,8 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
return;
}
final long contentLength = contentLength();
// issue#ICB1B8如果用户定义忽略contentLength头则不读取
final long contentLength = config.ignoreContentLength ? -1 : contentLength();
final FastByteArrayOutputStream out = new FastByteArrayOutputStream((int) contentLength);
copyBody(in, out, contentLength, null, this.config.ignoreEOFError);
this.body = new BytesResource(out.toByteArray());