1

I am working on a download manager in java.
My download manager working fine with some download link.
When a copy some download link given in a website and use it in my downloader it gives inappropriate result.
The main problem is actual URL of downloading file.
When I download that file directly in chrome what I see is the link chrome is using is different from the link I copied from website.
The URL I copied actually redirects to some other URL, which is the actual downloading link.
How can I solve this problem so that my downloader works fine.

I am using below code as suggested but its also not working.

private HttpURLConnection checkForRedirect(URL url) throws IOException{
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //conn.setReadTimeout(5000);
        conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
        conn.addRequestProperty("User-Agent", "Mozilla");
        conn.addRequestProperty("Referer", "google.com");

        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");

            // get the cookie if need, for login
            String cookies = conn.getHeaderField("Set-Cookie");

            // open the new connnection again

            conn = (HttpURLConnection) new URL(newUrl).openConnection();
            conn.setRequestProperty("Cookie", cookies);
            conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
            conn.addRequestProperty("User-Agent", "Mozilla");
            conn.addRequestProperty("Referer", "google.com");

            System.out.println("Redirect to URL : " + newUrl);

        }
        return conn;
    }

URL I am passing as argument is https://class.coursera.org/pythonlearn-003/lecture/download.mp4?lecture_id=19 and i am expecting redirected URL https://d28rh4a8wq0iu5.cloudfront.net/pythonlearn/recoded_videos/PY4INF-09-Dictionaries.1e3176607c9b11e3a3ad737af77ccaf6.mp4?response-content-type=application%2Foctet-stream&a=1&response-content-disposition=attachment%3B%20filename%3D%2211%2520-%25201%2520-%2520Chapter%25209%2520-%2520Dictionaries%2520%252837%253A38%2529.mp4%22%3B%20filename%2A%3DUTF-8%27%2711%2520-%25201%2520-%2520Chapter%25209%2520-%2520Dictionaries%2520%252837%253A38%2529.mp4

tichu
  • 13
  • 1
  • 6
  • What are you using for your download manager ? Are you using Apache HttpClient or plain HttpURLConnection for your download manager ? – Sariq Shaikh Dec 18 '14 at 16:18

1 Answers1

0

For your HttpURLConnection you can follow the redirect as shown here

Sariq Shaikh
  • 940
  • 8
  • 29
  • thanks :) i will try our solution one more question i don't know about Apache HttpClient??? should i learn about it?? is it more effective for downloader??? – tichu Dec 18 '14 at 16:24
  • Apache HttpClient is better except for few cases as described here http://stackoverflow.com/questions/4799151/apache-http-client-or-urlconnection Also you can see how convenient it is compare to HttpURLConnection to be used by checking some of the examples here http://www.mkyong.com/java/apache-httpclient-examples/ – Sariq Shaikh Dec 18 '14 at 16:30
  • What problem you are facing with the code ? The first URL you have posted requires authentication, I guess you are not doing the authentication while connecting through HttpUrlConnection. – Sariq Shaikh Dec 18 '14 at 17:40