Aspsine / MultiThreadDownload

Android Multi-Thread Download library
908 stars 285 forks source link

UnSupported response code:302 #81

Open ConCaRo opened 8 years ago

ConCaRo commented 8 years ago

with errorMessage: UnSupported response code:302.

And how to resolve it? Thanks a lot!

ConCaRo commented 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;
    }
pishguy commented 7 years ago

unfortunately this is not work for me, for example i can't download this image from net, for example:

https://kermany.com/wp-content/uploads/2016/03/dr-1.jpg