【问题标题】:Downloading a file from a redirection page从重定向页面下载文件
【发布时间】:2015-10-10 09:15:00
【问题描述】:

我将如何从重定向页面(它本身根据用户进行一些计算)下载文件。

例如,如果我希望用户下载游戏,我会使用 WebClient 并执行以下操作:

client.DownloadFile("http://game-side.com/downloadfetch/");

做起来没那么简单

client.DownloadFile("http://game-side.com/download.exe");

但如果用户点击第一个,它会重定向并下载它。

【问题讨论】:

标签: c#


【解决方案1】:

我认为,您应该使用像这样稍微定制的 WebClient 类。它将遵循代码 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");

【讨论】:

【解决方案2】:

据我所知,DownloadFile() 是不可能的;

你可以用这个

HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://game-side.com/downloadfetch/");    
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();

另见

Download file through code that has a redirect?

【讨论】:

  • 所以这个问题是重复的?那你为什么要回答呢?
猜你喜欢
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多