This commit is contained in:
Looly
2022-09-30 18:59:50 +08:00
parent 42a427193b
commit 4212d47bc2
4 changed files with 39 additions and 4 deletions

View File

@@ -1236,7 +1236,20 @@ public class HttpRequest extends HttpBase<HttpRequest> {
if (responseCode != HttpURLConnection.HTTP_OK) {
if (HttpStatus.isRedirected(responseCode)) {
setUrl(UrlBuilder.ofHttpWithoutEncode(httpConnection.header(Header.LOCATION)));
final UrlBuilder redirectUrl;
String location = httpConnection.header(Header.LOCATION);
if(false == HttpUtil.isHttp(location) && false == HttpUtil.isHttps(location)){
// issue#I5TPSY
// location可能为相对路径
if(false == location.startsWith("/")){
location = StrUtil.addSuffixIfNot(this.url.getPathStr(), "/") + location;
}
redirectUrl = UrlBuilder.of(this.url.getScheme(), this.url.getHost(), this.url.getPort()
, location, null, null, this.charset);
} else{
redirectUrl = UrlBuilder.ofHttpWithoutEncode(location);
}
setUrl(redirectUrl);
if (redirectCount < config.maxRedirectCount) {
redirectCount++;
// 重定向不再走过滤器

View File

@@ -0,0 +1,19 @@
package cn.hutool.http;
import cn.hutool.core.lang.Console;
import org.junit.Ignore;
import org.junit.Test;
import java.net.HttpCookie;
public class IssueI5TPSYTest {
@Test
@Ignore
public void redirectTest() {
final String url = "https://bsxt.gdzwfw.gov.cn/UnifiedReporting/auth/newIndex";
final HttpResponse res = HttpUtil.createGet(url).setFollowRedirects(true)
.cookie(new HttpCookie("iPlanetDirectoryPro", "123"))
.execute();
Console.log(res.body());
}
}