Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download from Redirected Url #31

Open
ghost1372 opened this issue Jan 28, 2021 · 15 comments
Open

Download from Redirected Url #31

ghost1372 opened this issue Jan 28, 2021 · 15 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ghost1372
Copy link
Contributor

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

@bezzad
Copy link
Owner

bezzad commented Jan 29, 2021

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

@bezzad bezzad self-assigned this Jan 29, 2021
@bezzad bezzad added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 29, 2021
@ghost1372
Copy link
Contributor Author

any news?
i found a article It may be useful
dotnet/runtime#23264

@ghost1372
Copy link
Contributor Author

ghost1372 commented Feb 11, 2021

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

@bezzad
Copy link
Owner

bezzad commented Feb 11, 2021

@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
Copy link
Owner

bezzad commented Feb 11, 2021

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.

@ghost1372
Copy link
Contributor Author

ghost1372 commented Feb 12, 2021

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
Copy link
Contributor Author

@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

@bezzad
Copy link
Owner

bezzad commented Mar 13, 2021

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

@ghost1372
Copy link
Contributor Author

any news?

@bezzad
Copy link
Owner

bezzad commented Jun 2, 2021

unfortunately nothing :)

@bezzad
Copy link
Owner

bezzad commented Oct 16, 2022

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.

@ghost1372
Copy link
Contributor Author

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

@WellitonToledo
Copy link

Seems I have a similar problem. 90% of times the download is corrupted..

await Downloader.DownloadFileTaskAsync("https://vl.actuar.net.br/ActuarAcesso", "somefile.zip");

@bezzad
Copy link
Owner

bezzad commented Oct 19, 2022

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

@hankb299
Copy link

What would you like me to do about this situation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants