Downloading file without direct link through C# Webclient

Viewed 364

I am trying to download a file but the problem is that the URL is not a direct link to the zip file, and my code gives me useless error.

This is the code:

var zipFile = new FileInfo(Path.Combine(directory.FullName, "temp.zip"));
var progressInfo =
    new ProgressBarInfo(
        $"{DateTime.Now:dd-MM-yyyy HH:mm:ss.ffff}\t[PROGRESS] Download IP2Location database");
UiManager.UiImplementation.ShowProgressBar(progressInfo);
try
{
    using (var webClient = new WebClient { Proxy = null })
    {
        var url = $"https://www.ip2location.com/download/?token={downloadToken}&file={DatabaseProduct}";
        webClient.DownloadProgressChanged +=
            (sender, args) =>
                progressInfo.ReportProgress(args.ProgressPercentage / 100d);
        using (var autoResetEvent = new AutoResetEvent(false))
        {
            webClient.DownloadFileCompleted += (sender, args) => autoResetEvent.Set();
            webClient.DownloadFileAsync(new Uri(url), zipFile.FullName);
            autoResetEvent.WaitOne();
        }
    }
}
finally
{
    progressInfo.Close();
}

If I change the URL variable to a direct link eg: https://speed.hetzner.de/10GB.bin then the code works fine

If I visit this URL: https://www.ip2location.com/download/?token=REDACTED&file=DB11LITE it will directly download a file named IP2LOCATION-LITE-DB11.CSV.ZIP

This is the response header when I visit the URL in my browser

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 13 Dec 2021 23:22:15 GMT
Content-Type: application/zip
Content-Length: 48726637
Connection: keep-alive
Expires: 0
Cache-Control: must-revalidate, post-check=0, pre-check=0
Pragma: public
Cache-Control: private
Content-Disposition: attachment; filename="IP2LOCATION-LITE-DB11.CSV.ZIP"
Content-Transfer-Encoding: binary
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer-when-downgrade
Content-Security-Policy: default-src * data: 'unsafe-eval' 'unsafe-inline';frame-ancestors 'self';
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
1 Answers

It's important to note that the Webclient class uses the RETR command to download an FTP resource. For an HTTP resource, the GET method is used. That means if you provide a URL that doesn't contains the correct parameters to a downloadable file, you gonna end up with some exceptions that are not handled because Webclient was replaced with System.Net.Http.HttpClient, that I recommend you use instead.

Below you can see a exemple of how the Webclient works, on your case you are getting "useless error" because you are on a async method. I would suggest to use the normal method like below to debug and get the correct exception.

public void downloadProgrambyURL(string url, string filename)
            {
                using (WebClient client = new WebClient())
                {
                    var pathVariable = "%USERPROFILE%\\Downloads\\" + filename;
                    var filePath = Environment.ExpandEnvironmentVariables(pathVariable);
                    client.DownloadFile(url, filePath);
                }
            }

Also, I'll let this link for your reference.

Related