My C# Windows Forms program needs to talk to servers that include multiple Set-Cookie
headers in HTTP responses.
.NET's HttpWebResponse
seems to concatenate the contents of multiple Set-Cookie
headers into one value - leaving the final value invalid. This results in sent cookies not being stored properly when using an associated CookieContainer
.
Example
HttpWebRequest
initiates a request and server replies with...
HTTP/1.1 200 OK
....
Set-Cookie: cookie1=123; Domain=127.0.0.1; Path=/; Expires=Wed, 13 Jan 2021 22:23:01 GMT
Set-Cookie: cookie2=456; Domain=127.0.0.1; Path=/somepath; Expires=Wed, 13 Jan 2021 22:23:01 GMT
....
...and HttpWebResponse
's header value (copied from VS Debugger) is:
Set-Cookie: cookie1=123; Domain=127.0.0.1; Path=/; Expires=Wed, 13 Jan 2021 22:23:01 GMT,cookie2=456; Domain=127.0.0.1; Path=/somepath; Expires=Wed, 13 Jan 2021 22:23:01 GMT
HttpWebResponse
only contains cookie1
in Cookies
property.
.NET Reflection on the original HttpWebRequest
's CookieContainer
to get all cookies (https://stackoverflow.com/a/15991071) also shows only cookie1
is parsed into an object and cookie2
is ignored.
Are there ways to get multiple Set-Cookie
headers handled properly without resorting to (significantly) lower-level or more complicated alternatives to HttpWebResponse
?