HttpRequest增加setFixedContentLength方法

This commit is contained in:
Looly 2024-09-14 15:31:22 +08:00
parent 975cfa0fb1
commit 277ecc90dc
2 changed files with 36 additions and 29 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.33(2024-09-13)
# 5.8.33(2024-09-14)
### 🐣新特性
* 【core 】 SyncFinisher增加setExecutorService方法issue#IANKQ1@Gitee
@ -11,7 +11,7 @@
* 【crypto 】 SM2解密时兼容GmSSL非压缩省略的04头的密文issue#IAP1QJ@Gitee
* 【core 】 兼容NumberUtil.add方法传入整型自动类型转换为浮点类型的精度丢失问题pr#3721@Github
* 【core 】 ModifierUtil明确注释并增加hasAllModifiers方法issue#IAQ2U0@Gitee
* 【http 】 HttpRequest增加setFixedLengthStreamingMode方法issue#3462@Github
* 【http 】 HttpRequest增加setFixedContentLength方法issue#3462@Github
### 🐞Bug修复
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题issue#3713@Github

View File

@ -238,6 +238,10 @@ public class HttpRequest extends HttpBase<HttpRequest> {
* 重定向次数计数器内部使用
*/
private int redirectCount;
/**
* 固定长度用于设置HttpURLConnection.setFixedLengthStreamingMode默认为0表示使用默认值默认值由HttpURLConnection内部决定通常为0
*/
private long fixedContentLength;
/**
* 构造URL编码默认使用UTF-8
@ -358,8 +362,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
* @return this
* @since 5.8.33
*/
public HttpRequest setFixedLengthStreamingMode(long contentLength){
this.httpConnection.setFixedLengthStreamingMode(contentLength);
public HttpRequest setFixedContentLength(long contentLength) {
this.fixedContentLength = contentLength;
return this;
}
@ -878,6 +882,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
/**
* 自动重定向时是否处理cookie
*
* @param followRedirectsCookie 自动重定向时是否处理cookie
* @return this
*/
@ -1239,6 +1244,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
.setInstanceFollowRedirects(false)
// 流方式上传数据
.setChunkedStreamingMode(config.blockSize)
// issue#3462 自定义body长度
.setFixedLengthStreamingMode(this.fixedContentLength)
// 覆盖默认Header
.header(this.headers, false);
@ -1392,7 +1399,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
*
* @return body
*/
private RequestBody createBody(){
private RequestBody createBody() {
// Write的时候会优先使用body中的内容write时自动关闭OutputStream
if (null != this.body) {
return ResourceBody.create(this.body);
@ -1410,9 +1417,9 @@ public class HttpRequest extends HttpBase<HttpRequest> {
private void sendMultipart() throws IOException {
final RequestBody body;
// issue#3158当用户自定义为multipart同时传入body则不做单独处理
if(null == form && null != this.body) {
if (null == form && null != this.body) {
body = ResourceBody.create(this.body);
}else{
} else {
final MultipartBody multipartBody = MultipartBody.create(this.form, this.charset);
//设置表单类型为Multipart文件上传
this.httpConnection.header(Header.CONTENT_TYPE, multipartBody.getContentType(), true);