HttpConfig增加配置setUseGetIfRedirect

This commit is contained in:
Looly
2024-09-06 16:45:12 +08:00
parent 5da69e1f40
commit cefc0fd559
3 changed files with 27 additions and 3 deletions

View File

@@ -96,9 +96,15 @@ public class HttpConfig {
boolean followRedirectsCookie;
/**
* issue#3719 如果为true则当请求头中Content-Type为空时使用默认的Content-Type即application/x-www-form-urlencoded
*
* @since 5.8.33
*/
boolean useDefaultContentTypeIfNull = true;
/**
* 当重定向时是否使用Get方式发送请求<br>
* issue#3722 部分请求要求重定向时必须使用Get方式发送请求
*/
boolean useGetIfRedirect;
/**
* 设置超时,单位:毫秒<br>
@@ -186,7 +192,7 @@ public class HttpConfig {
*/
public HttpConfig setHttpProxy(String host, int port) {
final Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(host, port));
new InetSocketAddress(host, port));
return setProxy(proxy);
}
@@ -322,6 +328,7 @@ public class HttpConfig {
/**
* 设置是否使用默认Content-Type如果请求中未设置Content-Type是否使用默认值
*
* @param useDefaultContentTypeIfNull 是否使用默认Content-Type
* @return this
* @since 5.8.33
@@ -330,4 +337,16 @@ public class HttpConfig {
this.useDefaultContentTypeIfNull = useDefaultContentTypeIfNull;
return this;
}
/**
* 重定向时是否使用GET方式
*
* @param useGetIfRedirect 重定向时是否使用GET方式
* @return this
* @since 5.8.33
*/
public HttpConfig setUseGetIfRedirect(boolean useGetIfRedirect) {
this.useGetIfRedirect = useGetIfRedirect;
return this;
}
}

View File

@@ -1313,9 +1313,13 @@ public class HttpRequest extends HttpBase<HttpRequest> {
redirectUrl = UrlBuilder.ofHttpWithoutEncode(location);
}
setUrl(redirectUrl);
if(config.useGetIfRedirect){
// since 5.8.33, issue#3722
setMethod(Method.GET);
}
if (redirectCount < config.maxRedirectCount) {
redirectCount++;
// 重定向不再走过滤器
// 重定向可选是否走过滤器
return doExecute(isAsync, config.interceptorOnRedirect ? config.requestInterceptors : null,
config.interceptorOnRedirect ? config.responseInterceptors : null);
}