What is the difference between CookieContainer and Response.Cookies collection?
Asked Answered
G

1

6

I have been doing some research on this and I couldn't find a straight answer.

Are there some cookies stored in the container that I can't get to using the Response.Cookies collection? How are cookies handled between requests using these objects? Are some cookies stored in the container but not others?

Ghislainegholston answered 25/9, 2012 at 22:32 Comment(0)
E
8

Judging by the tags you've used on this question, it looks like you're getting confused.

CookieContainer and CookieCollection are used with HttpWebRequest which is when your program is a HTTP client. CookieCollection stores cookies associated with a single domain name. CookieContainer stored all the cookies over all the domain names.

In ASP.NET, HttpRequest.Cookies is HttpCookieCollection and is used when your program is a HTTP server. It stores the cookies sent by the client to the server. Because there is only one domain name (i.e. yours) there is no need for a 2-dimensional collection.

The two sets of classes (CookieContainer and CookieCollection vs HttpCookieCollection) are totally unrelated with each other.

In this post I've provided some basic background on the cookie collection classes in .NET, but I don't understand your question. What is it you're trying to do?

Expurgatory answered 25/9, 2012 at 22:36 Comment(3)
Please provide a link to your post. I am just trying to understand the difference. I needed to log in to a site using these objects and carry through the session and authentication cookies for each requests. For some reason the CookieCollection did not contain any cookies but when I switched to CookieContainer it worked. I just wanted to understand why.Ghislainegholston
Is this an ASP.NET question, or a HttpWebRequest question, or are you attempting to use HttpWebRequest from within an ASP.NET application?Expurgatory
I send a request using HttpWebRequest to a server and get back an HttpWebResponse. I can get to cookies using HttpWebResponse.Cookies and/or CookieContainer. This questions was intended to get a bit more insight into what the difference is between the two (if any) given this scenario. If there is no difference then why have both?Ghislainegholston

© 2022 - 2024 — McMap. All rights reserved.