views:

378

answers:

5

I'm using webbrowser control to login any site. And then i want to download some sub page html using WebRequest (or WebClient). This links must requires authentication.

How to transfer Webbrowser authentication information to Webrequest or Webclient?

+1  A: 

If you can retrieve the necessary cookies from the WebBrowser control after they are set by the site you are logging into, you should be able to use those same cookies with WebRequest/WebClient.

This article outlines how to use cookies with a WebClient; you have to subclass it, but it's only a single override that's needed.

B.R.
I'm tring to get cookie. But webbrowser.document.cookie is null
ebattulga
@ebattulga - if the cookie is 'HttpOnly' then you can't retrieve it from javascript. http://msdn.microsoft.com/en-us/library/system.web.httpcookie.httponly.aspx
Giorgi
A: 

It is not easy to accomplish what you are trying to do. The server could be using one of two types of authentication with the client (browser)

1) Transport authentication 2) Forms based auth.

Transport auth: In this case, the authentication is done using the transport connection itself - here it will use custom HTTP headers with a multiway handshake to do the auth.

Forms based auth:This is the traditional auth that is done when you enter your credentials into a form.

In either case, the authentication might have already happened by the time your managed control is instantiated. As someone suggested you can steal the browsers cookies for that domain and use it with webclient but then it may or may not work the way you expect.

If all you want to do is download something, then I would see if I could use the browsers facilities for eg, XmlHttpRequest or some other ajax mechanism to download what you want as part of the DOM of the page that is hosting the control. Then you could read that content from your control, or you could also have the browser inject that content into your control by way of Javascript calling a method/property on your control.

[EDIT]

Find out (using Firebug plugin in firefox) exactly how the forms based auth is being done in the browser. Then you can write code to do exactly the same request/response that the browser is doing. To the website, the client will appear like any other browser based client. Then you should be able to download anything you want from the website.

Hope this helps.

feroze
+1  A: 

One possible way to do this is to get the cookie by using InternetGetCookie function, construct corresponding cookie object and use it for the CookieContainer

To retrieve HttpOnly cookies use InternetGetCookieEx

Here are some examples:

InternetGetCookie() in .NET

Download using Internet Explorer Cookies

Giorgi
That's not get all cookies
ebattulga
Try InternetGetCookieEx with INTERNET_COOKIE_HTTPONLY or INTERNET_COOKIE_THIRD_PARTY
Sheng Jiang 蒋晟
A: 

I've actually been through this same problem on the Windows Mobile platform, and the only thing that worked was to extend the WebBrowser control (using C++ :<) to capture the POST/GET vars before the request is sent.

This library may help you out:

http://www.codeproject.com/KB/miscctrl/csEXWB.aspx

"..the library implements the PassthroughAPP package by Igor Tandetnik, which enables the client to intercept all HTTP and HTTPS request and responses."

So although it is not possible to get the POST/GET vars used for your basic auth on a standard WebBrowser control, it would be possible if you use an extended control such as the sample I linked - in fact many "Extended WebBrowser" controls are created because of problems very similar to yours. Unfortunately, as far as I know you need to do this using unmanaged code/c++ :(.

vdoogs
A: 

You should be able to access the cookies of the WebBrowser control with .Document.Cookie then in your HTTPWebRequest you can add that cookie to its cookie container.
Here is an example (VB.NET because I'm most familiar there):

Dim browser As New WebBrowser()
/*Do stuff here to auth with your webbrowser and get a cookie.*/

Dim myURL As String = "http://theUrlIWant.com/"
Dim request As New HTTPWebRequest(myURL)
request.CookieContainer = New CookieContainer()
request.CookieContainer.SetCookies(myURL, browser.Document.Cookie)

And that should transfer the cookie from your WebBrowser control over to your HTTPWebRequest class.

Justin
browser.Document.Cookie is null
ebattulga
Have you connected and authenticated with your web browser control before reading the .Document.Cookie value?
Justin