mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
HttpConfig增加setUseDefaultContentTypeIfNull方法
This commit is contained in:
@@ -94,6 +94,11 @@ public class HttpConfig {
|
||||
* 自动重定向时是否处理cookie
|
||||
*/
|
||||
boolean followRedirectsCookie;
|
||||
/**
|
||||
* issue#3719 如果为true,则当请求头中Content-Type为空时,使用默认的Content-Type,即application/x-www-form-urlencoded
|
||||
* @since 5.8.33
|
||||
*/
|
||||
boolean useDefaultContentTypeIfNull = true;
|
||||
|
||||
/**
|
||||
* 设置超时,单位:毫秒<br>
|
||||
@@ -314,4 +319,15 @@ public class HttpConfig {
|
||||
this.followRedirectsCookie = followRedirectsCookie;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否使用默认Content-Type,如果请求中未设置Content-Type,是否使用默认值
|
||||
* @param useDefaultContentTypeIfNull 是否使用默认Content-Type
|
||||
* @return this
|
||||
* @since 5.8.33
|
||||
*/
|
||||
public HttpConfig setUseDefaultContentTypeIfNull(boolean useDefaultContentTypeIfNull) {
|
||||
this.useDefaultContentTypeIfNull = useDefaultContentTypeIfNull;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@@ -1358,7 +1358,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
private void sendFormUrlEncoded() throws IOException {
|
||||
if (StrUtil.isBlank(this.header(Header.CONTENT_TYPE))) {
|
||||
if (this.config.useDefaultContentTypeIfNull && StrUtil.isBlank(this.header(Header.CONTENT_TYPE))) {
|
||||
// 如果未自定义Content-Type,使用默认的application/x-www-form-urlencoded
|
||||
this.httpConnection.header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.toString(this.charset), true);
|
||||
}
|
||||
|
Reference in New Issue
Block a user