简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM