Gabriel
Gabriel

Reputation: 969

Download a file using WebBrowser Control

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.

Upvotes: 0

Views: 8674

Answers (2)

regex
regex

Reputation: 3601

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.

Upvotes: 1

therealmitchconnors
therealmitchconnors

Reputation: 2760

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

Upvotes: 0

Related Questions