最近在找一个单词接口,请求后发现会进行重定向,于是就想找到重定向之后的地址,再获取数据
重定向地址是存放在响应头的Location中
public void test() {
String source = "http://open.iciba.com/dsapi/";
HttpResponse response = HttpRequest.get(source).execute();
int status = response.getStatus();
if (status == 301) {
String location = response.header("Location");
System.out.println("重定向地址" + location);
}
System.out.println();
}
需要使用hutool请求工具类