java http请求 HttpURLConnection 302 重定向
HttpURLConnection con = null;
try {
String url ="http://.。。。.com";
// 获取文件流
con = (HttpURLConnection) new URL(url).openConnection();
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
con.setInstanceFollowRedirects(false);
if (con.getResponseCode() == 302) {
//如果会重定向,保存302重定向地址,以及Cookies,然后重新发送请求(模拟请求)
String location = con.getHeaderField("Location");
con.disconnect();
url = location;
con = (HttpURLConnection) new URL(url).openConnection();
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
}
}
catch (IOException e) {
}
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2021/07/28 03:21