Reputation: 87
How would I go about downloading a file from a redirection page (which itself does some calculations based on the user).
For example, if I wanted the user to download a game, I would use WebClient and do something like:
client.DownloadFile("http://game-side.com/downloadfetch/");
It's not as simple as doing
client.DownloadFile("http://game-side.com/download.exe");
But if the user were to click on the first one, it would redirect and download it.
Upvotes: 0
Views: 2891
Reputation: 3787
I think, you should go with slightly customized WebClient
class like that. It will follow code 300 redirects:
public class MyWebClient : WebClient
{
protected override WebResponse GetWebResponse(WebRequest request)
{
(request as HttpWebRequest).AllowAutoRedirect = true;
WebResponse response = base.GetWebResponse(request);
return response;
}
}
...
WebClient client=new MyWebClient();
client.DownloadFile("http://game-side.com/downloadfetch/", "download.zip");
Upvotes: 2
Reputation: 453
As far as I know this isn't possible with DownloadFile();
You could use this
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://game-side.com/downloadfetch/");
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
See also
Download file through code that has a redirect?
Upvotes: 1