Webbrowser control download file in session

I use WebBrowser control to navigate the login page and download the file. Since I cannot find a way to control the download automatically using the control, I use the WebClient class to try to achieve this.

The problem is that since the WebClient not in the same context / session as the browser that I load, this is a "Security Error" screen.

Any ideas on how to pass the context of a WebBrowser session WebBrowser a WebClient ?

+6
c # webbrowser-control webclient
source share
2 answers

It should just be a matter of emulating cookies and headers in a WebBrowser session and reusing them to impersonate the session in WebClient, but it looks like you are already configured along this path.

This is how I continue.

  • Get cookies and headers from WebBrowser.

    Cookies: You can get cookies from a WebBrowser session by handling the DocumentCompleted event of the WebBrowser control and analyzing the cookie set from the DocumentCompleted event.

    Headers: Use a proxy server such as Fiddler [www.fiddler2.com/] to read the headers so you know what is required on the server.

  • Use the identifier compiled above for WebClient.

    Headers: Iterate over all collected headers and make sure they are added to the web client using myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); eg

    Cookies: see this post .

+3
source share

After one week of searching Google for a solution, I found one that is so simple!

I tell you that silently uploading the file to the HTTPS URL, and managing your web browser just does it.

1) Log in using a web browser 2) Use this code to download.

 //build de URL string _url = "https://........." //define a download file name and location string _filename = @"C:\Users\John\Documents\somefile.pdf"; //create a webcliente WebClient cliente = new WebClient(); //do some magic here (pass the webbrowser cokies to the webclient) cliente.Headers.Add(HttpRequestHeader.Cookie, webBrowser1.Document.Cookie); //and just download the file cliente.DownloadFile(_urlpdf, _filename); 

He solved my problem

+11
source share

All Articles