Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CookieContainer.SetCookies only sets the first one

Tags:

c#

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

like image 615
Steve Avatar asked Feb 08 '23 21:02

Steve


1 Answers

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");
like image 121
Kapol Avatar answered Feb 15 '23 12:02

Kapol