Solved

Using cookies from HttpWebRequest in WebBrowser

Posted on 2011-02-25
10
3,205 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:tech1984
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:tech1984
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now