Downloader
Downloader copied to clipboard
Download from Redirected Url
Hi, Hope you are well It seems that it is not possible to download from the server that forwards the download link to another address. For example, the following address will not be downloaded
https://isubtitles.org/download/arrow/farsi-persian/1801961
If you try with Internet Download Manager, you will see that this link is transferred to:
https://file.isubtitles.org/2020/11/16/arrow-farsi-persian-1801961.zip
Hi @ghost1372 , Thanks for reporting this issue. I will check this as soon.
any news? i found a article It may be useful https://github.com/dotnet/runtime/issues/23264
This seems to be due to the .NET Core Design, i tested with .Net Framework and File Downloaded
var downloader = new DownloadService();
await downloader.DownloadFileAsync("https://isubtitles.org/download/arrow/farsi-persian/1801961", new DirectoryInfo(Environment.CurrentDirectory));
But in this case, downloader is not able to recognize the file name and extension, Instead of getting the name and extension of the second link, it getting the details of the first link
@ghost1372
I did a lot of research on this but concluded that this feature is build-in managed in .NET. If you pass true to the AllowAutoRedirect option, it should automatically redirect to different addresses, but in the path where you had a problem, there is something that I am working on, and if I conclude, I will inform you from here.
Of course, if you find something that can solve this problem, be sure to mention it here, maybe we can solve it faster. Thank you very much for following up.
Now, I tested the given URL in InternetDownloadManager and it can't download.

I don't know why, but the redirection problem solved by changing the FetchResponseHeaders() method of Request class to below codes:
private async Task FetchResponseHeaders()
{
try
{
// Previous codes
}
catch (WebException exp) when (exp.Response is HttpWebResponse response &&
response.StatusCode == HttpStatusCode.Found &&
response.SupportsHeaders)
{
// https://github.com/dotnet/runtime/issues/23264
var redirectLocation = exp.Response?.Headers["location"];
if (string.IsNullOrWhiteSpace(redirectLocation) == false)
{
Address = new Uri(redirectLocation);
await FetchResponseHeaders();
}
}
}
I push this code to develop branch. please test this branch and if it's ok then I publish a new version. Thanks a lot.
Thank you @bezzad
the links have been filtered. So use VPN for testing.
In .Net Core, the download problem is solved and the file is saved with the correct name and extension
But in .NET Framework, the file is saved with the wrong name and extension

@bezzad i found that this method can help us to get redirected url Since this redirect problem exists in .NET Core We can use #if preprocessor directives to solve this problem for .NET Core
#if NETCOREAPP
public static async Task<string> GetRedirectedUrl(string url)
{
//this allows you to set the settings so that we can get the redirect url
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
string redirectedUrl = null;
using (HttpClient client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
// ... Read the response to see if we have the redirected url
if (response.StatusCode == System.Net.HttpStatusCode.Found)
{
HttpResponseHeaders headers = response.Headers;
if (headers != null && headers.Location != null)
{
redirectedUrl = headers.Location.AbsoluteUri;
}
}
}
return redirectedUrl;
}
#endif
I want to change HttpWebRequest to HttpClient as soon. And after this change, I can fix this issue. Thanks for your feedback dear friend
any news?
unfortunately nothing :)
I solved this problem in the new version. But I did not find a link to do another test. But it was ok in the integration tests. Please check this new version (3.0.0-beta) and if there is a problem, write me the download link. Thanks.
I solved this problem in the new version. But I did not find a link to do another test. But it was ok in the integration tests. Please check this new version (3.0.0-beta) and if there is a problem, write me the download link. Thanks.
Tnx
Seems I have a similar problem. 90% of times the download is corrupted..
await Downloader.DownloadFileTaskAsync("https://vl.actuar.net.br/ActuarAcesso", "somefile.zip");
@WellitonToledo This issue is about Redirecting the download URL to another link. But I checked your mentioned address and it has no redirect URL. Also, it hadn't any other issues in downloading. Please explain more what is the problem and if your problem is not about the Redirect URL, so create a new issue. Thanks.
What would you like me to do about this situation