Open ConCaRo opened 8 years ago
Can workaround by using codes below to get redirect link.
public static String getRedirectLink(String url) {
String result = url;
try {
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
System.out.println("Request URL ... " + url);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
System.out.println("Response Code ... " + status);
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
System.out.println("Redirect to URL : " + newUrl);
result = newUrl;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
unfortunately this is not work for me, for example i can't download this image from net, for example:
with errorMessage: UnSupported response code:302.
And how to resolve it? Thanks a lot!