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