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