Downloader icon indicating copy to clipboard operation
Downloader copied to clipboard

Download from Redirected Url

Open ghost1372 opened this issue 4 years ago • 17 comments

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

ghost1372 avatar Jan 28 '21 16:01 ghost1372

Hi @ghost1372 , Thanks for reporting this issue. I will check this as soon.

bezzad avatar Jan 29 '21 09:01 bezzad

any news? i found a article It may be useful https://github.com/dotnet/runtime/issues/23264

ghost1372 avatar Feb 11 '21 16:02 ghost1372

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 avatar Feb 11 '21 16:02 ghost1372

@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.

bezzad avatar Feb 11 '21 17:02 bezzad

Now, I tested the given URL in InternetDownloadManager and it can't download.

image

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.

bezzad avatar Feb 11 '21 19:02 bezzad

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 1 But in .NET Framework, the file is saved with the wrong name and extension 2

ghost1372 avatar Feb 12 '21 06:02 ghost1372

@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

ghost1372 avatar Mar 11 '21 13:03 ghost1372

I want to change HttpWebRequest to HttpClient as soon. And after this change, I can fix this issue. Thanks for your feedback dear friend

bezzad avatar Mar 13 '21 08:03 bezzad

any news?

ghost1372 avatar Jun 01 '21 07:06 ghost1372

unfortunately nothing :)

bezzad avatar Jun 02 '21 08:06 bezzad

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.

bezzad avatar Oct 16 '22 09:10 bezzad

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

ghost1372 avatar Oct 16 '22 09:10 ghost1372

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 avatar Oct 18 '22 22:10 WellitonToledo

@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.

bezzad avatar Oct 19 '22 06:10 bezzad

What would you like me to do about this situation

hankb299 avatar Apr 25 '23 00:04 hankb299