2

I'm trying to download a file located on internet site and I know how to do with WebClient class.

But at this case which the site is re-directed to some web address and therefore, I'm considering to use Process.Start("URL address before re-directed") to avoid bit complexity to download through re-directed web address. I even succeeded to download the file through re-directed web address.

However, I'm curious and want to speed up my application by removing some intermediate steps if possible.

If I approach through Process.Start("URL address before re-directed"), the Download Prompt is shown to ask users like 'Do you want to Open? Save? Save_as?'.

And I don't want this Download Prompt is shown and want to save automatically to utilize the file in my application.(If possible, I want to indicate the location(path) to be saved.)

Thank you so much !

StepUp
  • 36,391
  • 15
  • 88
  • 148
Kay Lee
  • 922
  • 1
  • 12
  • 40

1 Answers1

2

Process.Start("Some URL") is just telling the OS to handle the URL. The OS sees an URL and gives it to the default browser. The default browser does whatever it is configured to do. So with Process.Start("Some URL") you can not force the default browser to not show the download prompt.

You are probably looking for HttpWebRequest.AllowAutoRedirect.

Example from MSDN:

HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();  

And of course you want a file:

using (var stream = myHttpWebResponse.GetResponseStream()) {
    using (var fileStream = new FileStream(filePath, FileMode.Create)) {
        stream.CopyTo(fileStream);
    }
}
Fabian S.
  • 909
  • 6
  • 20
  • Many thanks for your excellnet knowledges. I've tried your suggestion but the file contains the web address to be re-directed as I got already as My question seems maybe little confusing..? could you please make the code to download from re-directed web address? Thank you so much ! :) – Kay Lee May 09 '16 at 10:56
  • @KayLee So you are not talking about the good http redirect, but about the evil script redirect. Then you are probably out of luck... – Fabian S. May 10 '16 at 07:24
  • Dear Fabian, I just need to download from re-directed web address. I can't understand the meaning of Dark script redirect..And I've decided to follow my initial idea to download first like you and extract address for re-direction and stepping ahead. Hope goods to be with you ! :) – Kay Lee May 10 '16 at 08:00