How to download file from url that redirects?

前端 未结 2 1361
情深已故
情深已故 2020-12-12 02:04

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

2条回答
  •  青春惊慌失措
    2020-12-12 02:41

    I guess the easy option is simply this (after what you've got there.. and the URL you provided in place of http://www.contoso.com):

    using (var responseStream = myHttpWebResponse.GetResponseStream()) {
        using (var fileStream = 
                  new FileStream(Path.Combine("folder_here", "filename_here"), FileMode.Create)) {
            responseStream.CopyTo(fileStream);
        }
    }
    

    EDIT:

    In fact, this won't work. It isn't a HTTP redirect that downloads the file. Look at the source of that page.. you'll see this:

    
    

    It basically uses the browser to redirect. Unfortunately what you're trying to do won't work.

提交回复
热议问题