Http 301和302重定向的目标地址和状态
最编程
2024-01-02 07:53:27
...
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();
}