Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using cookies from HttpWebRequest in WebBrowser

Posted on 2011-02-25
10
Medium Priority
?
3,784 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 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