The Notorious Yet Unaswered Issue of Downloading a File When Windows Security Is Required

The notorious yet unaswered issue of downloading a file when windows security is required

  1. Not possible, AFAIK. WebClient and WebBrowser use different layers to access web. WebClient uses WinHTTP, WebBrowser uses UrlMon. Thus, they would have separate sessions (including the authentication cache).

  2. That's possible, just use any of UrlMon APIs to download the file, e.g. URLDownloadToFile or URLDownloadToCacheFile. I've just answered a similar question.

On a side note, you don't have to feed in keystrokes to provide authentication credentials. You can implement IAuthenticateEx on WebBrowser site object for this. Here is more info.

Downloading a file from the web to be attached to an outlook message sent from client

Using a customized browser, yes. (If your client runs windows and has office installed...)

For example, create a .Net winform-program with a IE-browser-control in it.

Add hook to IE-control to get when user clicks a specific a-link.

Then perform a download of file from winform-program, use office-api/automation/3rdpart-control from winform-program to fire up a new mail and add attachment to the mail.

System.Net.Webclient only download 81.4 MB (85,363,075 bytes)

It was internal network proxy which was for some reason dropping the request after 81.4 MB (85,363,075 bytes) were transfered. But the weird thing is webclient was not throwing any exception and was pretending that download was successful.

Fix is to null the proxy so request is processed without going through proxy.

$client.proxy=$null  # get rid of proxy
$client.DownloadFile( $Url, $destination )

WebBrowser Control download file in session

It should be simply a matter of emulating the cookies and headers in the WebBrowser session and re-using them to impersonate the session in WebClient, but it looks like you're already hot on that path.

Here's how I'd proceed.

  1. Get cookies and headers from WebBrowser.

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

    Headers: Use a proxy like Fiddler [www.fiddler2.com/] to read the headers so you'll know what's required by the server.

  2. Utilize identity collected above for WebClient.

    Headers: Iterate through all your collected headers and be sure they are added to the webclient using myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); for example

    Cookies: See this post.



Related Topics



Leave a reply



Submit