473,721 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CookieContainer is driving me nuts, webResponse.Coo kies always hascount of zero after first webRequest

OK I logon to a web site and I manage to get an SMSESSION cookie that I
then store in a variable called _session (a class scoping variable). I
do this by calling a logon URL and setting a cookie to SMCHALLANGE=YES
to allow me to obtain a session. I then iterate the cookie collection
to extract the SMSESSION value. All is good (so far). This is when the
sky turns grey and the rain starts to fall. I then use this SMSESSION
in a web request and I get a response from the web server but I'm unable
to get any cookies. webResponse.Coo kies collection from here on in
always has a count of zero. Whether I try using a new cookie container,
re-using the old one, no matter what I do I seem to be unable to
retrieve any subsequent cookies. I'm starting to think it may be better
to iterate the HTTP headers and get the values myself. The SMSESSION
value changes on each request and so not being able to get the updated
value is a major issue.
-- code snippet follows --

HttpWebRequest webRequest = (HttpWebRequest )WebRequest.Cre ate(url);
webRequest.Meth od = "POST";
webRequest.Cont entType = "applicatio n/x-www-form-urlencoded";

webRequest.Cook ieContainer = new CookieContainer ();
webRequest.Cook ieContainer.Set Cookies(webRequ est.RequestUri, _session);

ASCIIEncoding encoding = new ASCIIEncoding() ;
byte[] data = encoding.GetByt es(payload);
webRequest.Cont entLength = data.Length;
Stream newStream = webRequest.GetR equestStream();

newStream.Write (data, 0, data.Length);
newStream.Close ();

HttpWebResponse webResponse;
webResponse = (HttpWebRespons e)webRequest.Ge tResponse();

foreach (Cookie c in webResponse.Coo kies)
if (c.Name == "SMSESSION" )
_session = c.ToString();

-- snip --
Jun 28 '06 #1
1 8199

OK I cant work out why the cookie container is always empty but I wrote
my own cookie getter and it works ok now. Since the site was working I
knew it had to be returning the cookies in the headers even if the
WebRepsonse.Coo kies had a count of 0. i.e. had to be that the
CookieContainer wasn't working correctly. Sure enough I was right :-)

Thought I'd post this code here incase it helps anyone else in the same

foreach (string key in webResponse.Hea ders.Keys)
string[] values = webResponse.Hea ders.GetValues( key);
for (int i = 0; i < values.Length; i++)
if (key == "Set-Cookie")
if (values[i].Contains("SMSE SSION"))
_session = values[i];
_log.Info("new session is:" + _session);
Jun 29 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the idea that costs nothing and it is inheritently built into the Framework. So here is the solution... When you use the WebRequest and WebResponse classes to obtain graphics from a web site, these classes have built-in decoding for JPEG 2000 files....
by: Nurchi BECHED | last post by:
Hello, everyone. I am stuck with the following: There is a webpage which contains a webform, which contains text fields, and submits the data from current page to another one. I need to submit the same data to a webpage from a Windows Form.
by: Tony Archer | last post by:
Here's my problem... When I use this code to hit the site in question I'm redirected to another SSL login page. If processed successfully the page creates a cookie and then redirects you to the original request. However, my code cannot seem to hold onto the cookie it creates. Does anyone know how to do this?
by: guyhey | last post by:
I am accessing a site that passes the following headers: Set-Cookie: ASP.NET_SessionId=pwdciy45i0c5me45urxjfoi0; path=/ Set-Cookie: LastSessID=pwdciy45i0c5me45urxjfoi0; expires=Tue, 01-Mar-2016 20:07:11 GMT; path=/ Set-Cookie: pubAccID=04; expires=Tue, 01-Mar-2016 20:07:11 GMT; path=/ I have verified this using ieHttpHeaders When I use asp.net
by: rlueneberg | last post by:
I am totally confused. Can someone please illuminate what is going on under the hood in this piece of code from John Lewis. My main confusion is how the cookieContainer can be passed to the subsequent request if it is not assigned anywhere? So far this is what I understand: -Creates New cookieContainer CookieContainer cookieContainer = new CookieContainer();
by: archana | last post by:
Hi all, I am facing very strange problem while using webrequest. What i am doing is i have one url which i want to validate. so i am using webrequest and webresponse. My code is as below:- try {
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a CookieContainer full of Cookies. I want to loop through the CookieContainer to display the cookies? How can I do this. I know how to loop through a CookieCollection and display the cookies. How can I turn a CookieContainer into a CookieCollection?
by: KyleUbenk | last post by:
Well I have a multi-threaded program which downloads URL sources from the web. Im doing this multiple times at once. The only problem I see is that sometimes it takes a while to download the sourceWebResponse ServerResponse = webrequest.GetResponse(); and only one webresponse can happen at once because the rest of the threads seem to wait on the first webresponse to finish. Is there a way around this? I hope you understand what my problem is. ...
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test what is going on. I'm not getting any errors right now, but code is not working either - not reading the post in second form or else the first form is not sending it correctly....
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.