Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Strange behavior with HttpCookieCollection

Reply
Thread Tools

Strange behavior with HttpCookieCollection

 
 
adlaird@earthlink.net
Guest
Posts: n/a
 
      07-19-2003
Hi,

I created a simple webform, put a test button on it and implemented a
click handler that looks like so:

private void btnTest_Click(object sender, System.EventArgs e)
{
HttpCookieCollection oCookReq =
HttpContext.Current.Request.Cookies;
HttpCookieCollection oCookResp =
HttpContext.Current.Response.Cookies;

int n1 = oCookReq.Count;
int n2 = oCookResp.Count;
bool bIsNull = (oCookResp["someCookieName"] == null);
int n3 = oCookReq.Count;
int n4 = oCookResp.Count;

Response.Write(string.Format("Before: Request = {0}, Response
= {1}<br>", n1, n2));
Response.Write(string.Format("After: Request = {0}, Response
= {1}<br>", n3, n4));
}


When I click the button the test output on the page is:

Before: Request = 1, Response = 0
After: Request = 2, Response = 1

Can anyone explain why the mere act of looking for a cookie in the
response collection would cause one to get added to both the response
and request collections? I tried the test again using a new
HttpCookieCollection (i.e. not one from the current context) and do
not get this behavior. Clues?

Thanks much...

- A

 
Reply With Quote
 
 
 
 
Chris R. Timmons
Guest
Posts: n/a
 
      07-19-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
news:(E-Mail Removed):

> Hi,
>
> I created a simple webform, put a test button on it and
> implemented a click handler that looks like so:
>
> private void btnTest_Click(object sender, System.EventArgs e)
> {
> HttpCookieCollection oCookReq =
> HttpContext.Current.Request.Cookies;
> HttpCookieCollection oCookResp =
> HttpContext.Current.Response.Cookies;
>
> int n1 = oCookReq.Count;
> int n2 = oCookResp.Count;
> bool bIsNull = (oCookResp["someCookieName"] == null);
> int n3 = oCookReq.Count;
> int n4 = oCookResp.Count;
>
> Response.Write(string.Format("Before: Request = {0},
> Response
>= {1}<br>", n1, n2));
> Response.Write(string.Format("After: Request = {0},
> Response
>= {1}<br>", n3, n4));
> }
>
>
> When I click the button the test output on the page is:
>
> Before: Request = 1, Response = 0
> After: Request = 2, Response = 1
>
> Can anyone explain why the mere act of looking for a cookie in
> the response collection would cause one to get added to both the
> response and request collections? I tried the test again using
> a new HttpCookieCollection (i.e. not one from the current
> context) and do not get this behavior. Clues?


You've stumbled upon a frustrating (and, AFAIK, undocumented) aspect
of the ASP.NET cookies response collection. Simply querying the
collection will create the cookie if it doesn't exist.

Here's an article that explains this in more depth:

http://www.codeproject.com/aspnet/AspNetCookies.asp

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird strange behavior... Jim Firefox 5 11-17-2005 03:09 PM
Firefox 1.04 and Strange Find Behavior Thomas Firefox 5 06-28-2005 08:40 PM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
strange behavior when using 'read' sstark Perl 0 03-06-2005 02:27 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments