473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CookieContainer is driving me nuts, webResponse.Cookies 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.Cookies 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.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.SetCookies(webRequest.R equestUri, _session);

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(payload);
webRequest.ContentLength = data.Length;
Stream newStream = webRequest.GetRequestStream();

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

HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();

foreach (Cookie c in webResponse.Cookies)
{
if (c.Name == "SMSESSION")
{
_session = c.ToString();
break;
}
}

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

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.Cookies 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
boat.

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

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

Similar topics

0
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...
1
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...
0
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...
1
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...
5
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...
0
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:-...
4
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...
3
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.