Steve
Steve

Reputation: 11963

CookieContainer.SetCookies only sets the first one

According to MSDN CookieContainer.SetCookies should

Adds Cookie instances for one or more cookies from an HTTP cookie header to the CookieContainer for a specific URI

which implies that it should work for multiple cookies, but when I do

_cookieContainer.SetCookies(new Uri("http://localhost"), "a=a;b=b");

and later try to retrieve the cookies using

_cookieContainer.GetCookies(new Uri("http://localhost"));

I only get one cookie entry which is a=a

I thought it might be that the cookie header format is wrong, so I manually added two cookies using .Add method, and later try to get the header by calling .GetCookieHeader , I get exactly the same string "a=a;b=b".

Did I miss anything or did I just find a .NET bug? I am currently using

VS2015 - v14.0.23107.0,
.NET - 4.6 4.6.00081

Upvotes: 6

Views: 7224

Answers (2)

Kapol
Kapol

Reputation: 6463

Why don't you try to pass the second parameter the same way as MSDN suggests on the documentation site of SetCookies:

cookieHeader

Type: System.String

The contents of an HTTP set-cookie header as returned by a HTTP server, with Cookie instances delimited by commas.

_cookieContainer.SetCookies(new Uri("http://localhost"), "a=a,b=b");

Upvotes: 9

Edmund Schweppe
Edmund Schweppe

Reputation: 5132

You're using semicolons to delimit your cookies, but the MSDN docs state:

SetCookies pulls all the HTTP cookies out of the HTTP cookie header, builds a Cookie for each one, and then adds each Cookie to the internal CookieCollection that is associated with the URI. The HTTP cookies in the cookieHeader string must be delimited by commas.

(emphasis added)

Upvotes: 3

Related Questions