简体   繁体   English

从重定向页面下载文件

[英]Downloading a file from a redirection page

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: 例如,如果我希望用户下载游戏,则可以使用WebClient并执行以下操作:

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. 但是,如果用户单击第一个,它将重定向并下载。

I think, you should go with slightly customized WebClient class like that. 我认为,您应该使用稍微自定义的WebClient类。 It will follow code 300 redirects: 它将遵循代码300重定向:

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");

As far as I know this isn't possible with DownloadFile(); 据我所知,使用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? 通过具有重定向的代码下载文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM