Solved

Using cookies from HttpWebRequest in WebBrowser

Posted on 2011-02-25
10
3,614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 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