How can I alter my C# download method so that it gets the final download link from a given link that it redirects to the it. Right now I'm interested in getting the last portable version of CCleaner which can be taken from https://www.ccleaner.com/ccleaner/download/portable using a browser.
this is my current download method:
public void downloadFile(String address, String filename)
{
WebClient down = new WebClient();
down.Headers.Add(HttpRequestHeader.UserAgent,"Mozilla/5.0 (compatible; http://example.org/)");
down.DownloadFileAsync(new Uri(address), filename);
}
Of course using this on the given link results in the download of a file which seems to be the html page itself. My project is and needs to be net3.5 and the download has to be async. Any idea how I could pull this off?
You should notice, that it is not a redirect. It is triggered by JavaScript code. The page contains the url you need, but you have to extract it yourself. Something like this could work, however you should add some checks for the regexp:
WebClient down = new WebClient();
down.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (compatible; http://example.org/)");
var result = down.DownloadString(new Uri("https://www.ccleaner.com/ccleaner/download/portable"));
var download = Regex.Match(result, "data-download-url=\"(.*?)\"").Groups[1].Value;
var uri = new Uri(download);
down.DownloadFileAsync(uri, Path.GetFileName(download));