fix redirect bug

This commit is contained in:
looly
2021-12-25 12:49:21 +08:00
parent c51bc90146
commit ce504c2a3e
5 changed files with 56 additions and 17 deletions

View File

@@ -149,4 +149,23 @@ public class HttpRequestTest {
Console.log(execute.body());
}
@Test
@Ignore
public void followRedirectsTest(){
// 从5.7.19开始关闭JDK的自动重定向功能改为手动重定向
// 当有多层重定向时JDK的重定向会失效或者说只有最后一个重定向有效因此改为手动更易控制次数
// 此链接有两次重定向当设置次数为1时表示最多执行一次重定向即请求2次
String url = "http://api.rosysun.cn/sjtx/?type=2";
// String url = "https://api.btstu.cn/sjtx/api.php?lx=b1";
// 方式1全局设置
HttpGlobalConfig.setMaxRedirectCount(1);
HttpResponse execute = HttpRequest.get(url).execute();
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
// 方式2单独设置
execute = HttpRequest.get(url).setMaxRedirectCount(1).execute();
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
}
}