http返回301或302状态,如何拿到重定向后的地址,如何拿到最终的请求状态呢?无论你用什么http工具,只要从header中拿到location属性的值就可以了,请参考下面的代码
Integer RESPONSE_CODE = 0;
try {
URL url = new URL(imgUrl);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
RESPONSE_CODE = urlcon.getResponseCode();
//重定向请求处理
if (RESPONSE_CODE == HttpURLConnection.HTTP_MOVED_PERM || RESPONSE_CODE == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = urlcon.getHeaderField("location");
URL url2 = new URL(location);
HttpURLConnection urlcon2 = (HttpURLConnection) url2.openConnection();
RESPONSE_CODE = urlcon2.getResponseCode();
}
} catch (Exception e) {
e.printStackTrace();
}