Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

c# - How to download file from url that redirects?

i'm trying to download a file from a link that doesn't contain the file, but instead it redirects to another (temporary) link that contains the actual file. The objective is to get an updated copy of the program without the need to open a browser. The link is:

http://www.bleepingcomputer.com/download/minitoolbox/dl/65/

I've tried to use WebClient, but it won't work:

private void Button1_Click(object sender, EventArgs e)
{
      WebClient webClient = new WebClient();
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
      webClient.DownloadFileAsync(new Uri("http://www.bleepingcomputer.com/download/minitoolbox/dl/65/"), @"C:DownloadsMiniToolBox.exe");
}

After searching and trying many things i've found this solution that involves using HttpWebRequest.AllowAutoRedirect.

Download file through code that has a redirect?

// Create a new HttpWebRequest Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");    
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();

It seems that's exactly what i'm looking for, but i simply don't know how to use it :/ I guess the link is a parameter of WebRequest.Create. But how can i retrieve the file to my directory? yes, i′m a noob... Thanks in advance for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I switched from a WebClient based approach to a HttpWebRequest too because auto redirects didn't seem to be working with WebClient. I was using similar code to yours but could never get it to work, it never redirected to the actual file. Looking in Fiddler I could see I wasn't actually getting the final redirect.

Then I came across some code for a custom version of WebClient in this question:

class CustomWebclient: WebClient
{
  [System.Security.SecuritySafeCritical]
  public CustomWebclient(): base()
  {
  }

  public CookieContainer cookieContainer = new CookieContainer();

  protected override WebRequest GetWebRequest(Uri myAddress)
  {
    WebRequest request = base.GetWebRequest(myAddress);
    if (request is HttpWebRequest)
    {
      (request as HttpWebRequest).CookieContainer =   cookieContainer;
      (request as HttpWebRequest).AllowAutoRedirect = true;
    }
    return request;
  }
}

The key part in that code is AllowAutoRedirect = true, it's supposed to be on by default according to the documentation, which states:

AllowAutoRedirect is set to true in WebClient instances.

but that didn't seem to be the case when I was using it.

I also needed the CookieContainer part for this to work with the SharePoint external URLs we were trying to access.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...