Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using cookies from HttpWebRequest in WebBrowser

Posted on 2011-02-25
10
Medium Priority
?
3,933 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

879 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