0

i'm trying to automate the file download from a web page, the problem is, that web return directly the file. for example, http://www.someweb.com/generatefile.aspx, when i navigate to that page, a file is returning. I tried with WebClient, but didn't work, always give error 500, however if i try through whit IExplorer or Firefox, work well. So, i trying with WebBrowser control, which i made all other things, such as login to web.

WebClient client = new WebClient();
client.DownloadFile(url, "D:/ss.pdf");  //Here error 500

and with WebBrowser

WebBrowser wb = new WebBrowser();
wb.FileDownload += new EventHandler(wb_FileDownload); //in the event handler i not found how save the file
wb.Navigate(url);

Thanks for any help.

Gabriel
  • 879
  • 1
  • 13
  • 33
  • A late answer for future references: `URLDownloadToFile` or `URLDownloadToCacheFile` [can be used for this](http://stackoverflow.com/a/19025793/1768303). – noseratio Sep 26 '13 at 15:34

2 Answers2

1

You'll need to do a bit more configuring, but I would recommend using the HttpWebRequest instead.

If you know the expected file type, then you can create a file on the filesystem with the known type and get a stream reference for it. Then just write the response stream returned by HttpWebRequest::GetResponse().

I've tried using the classes you mentioned in the past, but always fall back on using HttpWebRequest and HttpWebResponse. As I said earlier, they are a bit more low level, but in a lot of scenarios, the additional control over the low level HTTP functions has been helpful in my experience.

regex
  • 3,585
  • 5
  • 31
  • 41
0

Have you tried using an FTPWebRequest? The WebBrowser is made for GUI style interaction, not really for automation...

therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36