Solved

Using cookies from HttpWebRequest in WebBrowser

Posted on 2011-02-25
10
3,407 Views
Last Modified: 2012-05-11
I am writing an app that logs in to a website using HttpWebRequest and stores the cookies in a CookieContainer.  Most of the time I will not need to switch to the WebBrowser control, but when I do, I would like to be able to use the same cookies that I have already gotten so I don't have to login to the site all over again.

Can someone explain/ give an example on how to transfer the cookies from the CookieContainer to the WebBrowser.

Thanks!!
0
Comment
Question by:tech1984
  • 5
  • 4
10 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34982842
Please refer(have good example):
http://msdn.microsoft.com/en-us/library/dd920298(v=vs.95).aspx

Let me know if you are looking for different one.
0
 

Author Comment

by:tech1984
ID: 34982906
Unless I am missing something, this example just uses the CookieContainer with the HttpWebRequest object (which I am doing successfully).  I don't see how to transfer those cookies to a WebBrowser control.
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34982937
If you are using the Navigate Method from the webbrowser control, so the 4th parameter are the cookies:
webBrowser1.Navigate("http://www.somewhere.com", "", null, "Cookie: " + CC.GetCookieHeader("www.somewhere.com") + "\n");

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:tech1984
ID: 34983055
Passing the cookie in the Navigate Method didn't seem to work.  I confirmed that the GetCookieHeader is returning a cookie, but it still doesn't log me into the page on the WebBrowser side.
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34983107
What is the string of the cookie?(remember to modify some letters before pasting it here)
See if it is the same as the one you get when you visit the site through a common browser and write down on url:
javascript:alert(document.cookie);

Open in new window

(press enter after that and you'll see the cookies that the browser use)

Remember, just compare the SHAPE, not the values as they will not be equals.
0
 

Author Comment

by:tech1984
ID: 34983243
They are similar, but when viewing it in a browser there is more information.   The following is part of the cookie from the browser.

PHPSESSID=1234567892c1dbbc81d123456789; CakeCookie[lastPage]=MyLastPage; __utma=99006088.1840565748027007600.1250004032.1290040255.1290065183.141; CakeCookie[resultperpage]=50;

When I view the cookie from the Cookie Container, I do not get the "__utma=.... " section.  The cookie goes on and the following is at the end which is also not in my Cookie Container

__utmc=99006088; __utmb=99856088.2.10.1298665183; __utmz=99006088.1298005183.141.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34984958
Than this approach will not work until we get the complete cookie beginning with the sessionID which is very important.
I'll make a research and return back as soon as i get an way to work this out.
0
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 500 total points
ID: 34985060
Well, the best way is to work out the way you get the cookies.
You are using the GetResponse method and the WebResponse class right?

I'll show you an http cookie being set by the server:
Set-Cookie: bb_lastvisit=1298658572; expires=Sun, 26-Feb-2012 00:28:55 GMT; path=/
Set-Cookie: bb_lastactivity=0; expires=Sun, 26-Feb-2012 00:28:55 GMT; path=/
Set-Cookie: bb_sessionhash=5221be42c1d7e9952ba9d525de0d4015; path=/; HttpOnly

Open in new window


As you can see, you can get the correct cookies using:
response.Headers.Get("Set-Cookie");

Open in new window


Which will return in a String the session which is responsible for the persistence of your login.

If it is not the way you are using, then let me know which way is it.
0
 

Assisted Solution

by:tech1984
tech1984 earned 0 total points
ID: 34988088
I am using the GetResponse and WebRequest.  I tried setting the cookie from the string returned from the response.Headers.Get("Set-Cookie"); by two different methods.

1.  I tried to pass it during the Navigation Method
2.  I tried setting it in the WebBrowser.Document.Cookie Property

Neither method worked, but when running the javascript command, the structure of the cookie looked much better when setting it via the Document.Cookie Property.

I am currently going as far as seeing if I can autologin with a WebBrowser control and grab the cookie from the Document.Cookie then.

If you have any other suggestion, I am open to try them.
0
 

Author Closing Comment

by:tech1984
ID: 35154480
I wasn't ever able to get the cookies to work between the controls.  In the end, I found it easier to "auto-login" when I needed to use the WebBrowser.  It wasn't as convenient, but it accomplished what I needed at the time.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question