mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix redirect bug
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user