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

@@ -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());