Questions tagged [cookiecontainer]

Provides a container for a collection of CookieCollection objects.

In C#:

A CookieContainer is a data structure that provides storage for instances of the Cookie class, and which is accessed in a database-like manner. The CookieContainer has a capacity limit that is set when the container is created or changed by a property.

One might use a CookieContainer in conjunction with a CookieCollection object for persisting HTTP cookies between HttpWebRequests and HttpWebResponses.

Some example usage:

CookieContainer container = new CookieContainer();
CookieCollection cookies = new CookieCollection();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://somesite.com/login");
request.CookieContainer = container;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies; // capture the cookies from the response

request = (HttpWebRequest)WebRequest.Create("http://somesite.com/profile");
request.CookieContainer = container;
// capture the cookies from the response for sending with a request
request.CookieContainer.Add(cookies);

response = (HttpWebResponse)request.GetResponse();
// capture the cookies from the response for sending with a request
cookies = response.Cookies; 
107 questions
158
votes
5 answers

Using CookieContainer with WebClient class

I've previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no built-in method like there is for HttpWebRequests (request.CookieContainer). How…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
34
votes
4 answers

C#: Writing a CookieContainer to Disk and Loading Back In For Use

I have a CookieContainer extracted from a HttpWebRequest/HttpWebResponse session named CookieJar. I want my application to store cookies between runs, so cookies collected in the CookieContainer on one run of the program will be used the next run,…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
29
votes
4 answers

How can I get all Cookies of a CookieContainer?

I want to export a CookieContainer to JSON using Newtonsoft.Json but unfortunately CookieContainer hasn't an enumerator or stuff, so I can't cycle through it ... Edit: With my posted solution it would be something like this: private static void…
christophrus
  • 1,170
  • 2
  • 14
  • 27
27
votes
3 answers

Sending cookies using HttpCookieCollection and CookieContainer

I want to tunnel through an HTTP request from my server to a remote server, passing through all the cookies. So I create a new HttpWebRequest object and want to set cookies on it. HttpWebRequest.CookieContainer is type System.Net.CookieContainer…
Mike
  • 5,560
  • 12
  • 41
  • 52
22
votes
4 answers

HttpWebRequest cookie with empty domain

I have an ASP.NET MVC action that sends a GET request to another server via HttpWebRequest. I'd like to include all cookies in the original action's request in the new request. Some of the System.Web.HttpCookies in the original request have empty…
MikeWyatt
  • 7,842
  • 10
  • 50
  • 71
17
votes
6 answers

CookieContainer bug?

I'm confused how CookieContainer handles domain, so I create this test. This test shows cookieContainer doesn't return any cookie for "example.com" but according to RFC it should return at least 2 cookies. Isn't it a bug? How make it to work? Here…
Salar
  • 495
  • 3
  • 6
  • 14
14
votes
2 answers

handling a comma inside a cookie value using .net's (C#) System.Net.Cookie

I'm creating a client to visit a website and log in + do some tasks automatically, however they recently updated their cookies to (for whatever reason...) contain a comma inside their identification cookie. So for example, the Cookie will have a…
theootz
13
votes
3 answers

Use cookies from CookieContainer in WebBrowser

Is there any way that I can actually use the cookies from a cookie container (taken from a WebRequest previously) and use them in a WebBrowser control? If so, how would I do this? This is for a Winforms application in C#.
Alex
  • 2,114
  • 9
  • 25
  • 34
12
votes
5 answers

Is .NET System.Net.CookieContainer thread safe?

Is the .NET class System.Net.CookieContainer thread safe? --Update: Turnkey answered-- Is there any way to ensure thread safeness to variables which are modified during asynchronous requests (ie. HttpWebRequest.CookieContainer)? Is there any…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
10
votes
3 answers

powershell httpwebrequest GET method cookiecontainer problem?

I'm trying to scrape a website that has user authentication. I am able to do a POST to send my login and stores a cookie. However, after the login I get a 403 error when trying to access the protected page. $url =…
10
votes
1 answer

HttpWebRequest.Headers.Add("Cookie",value) vs HttpWebRequest.CookieContainer

When I get response from HttpWebRequest with HttpWebRequest.Headers.Add("Cookie",value) vs HttpWebRequest.CookieContainer, and results are difference. So, What is the difference between they are, and when to use them.
Mr.LamYahoo
  • 1,536
  • 13
  • 28
9
votes
1 answer

CookieContainer handling of paths (Who ate my cookie?)

I'm working on a project that involves some basic web crawling. I've been using HttpWebRequest and HttpWebResponse quite successfully. For cookie handling I just have one CookieContainer that I assign to HttpWebRequest.CookieContainer each time. I…
Andy
  • 307
  • 4
  • 13
8
votes
6 answers

Managing cookies in a WPF WebBrowser control?

Is there a way to read/write the cookies that a WebBrowser control uses? I am doing something like this... string resultHtml; HttpWebRequest request = CreateMyHttpWebRequest(); // fills http headers and stuff HttpWebResponse response =…
kenwarner
  • 28,650
  • 28
  • 130
  • 173
7
votes
1 answer

cookieContainer with Typed Clients

Need to make a request to server which need specific cookies. Able to do this using HTTP client and handler with cookiecontainer. By using Typed clients, not able to find a way to set cookiecontainer. Using httpclient: var cookieContainer = new…
7
votes
4 answers

WCF Web Service Client using a CookieContainer

I have developed a small C# form application which calls a web service. Everything works nicely but I need to keep state and to do that I need to use a CookieContainer if I am not mistaken. I created the Service Reference by using the "Add Service…
Timmo
  • 3,142
  • 4
  • 26
  • 43
1
2 3 4 5 6 7 8