I made a server that listen to a port for POST requests. It handles a JSON and it deserializes to a class containing some strings and a CookieCollection
All correct there, but when I pass the CookieCollection to a CookieContainer and use that container on a request it is sending without the cookies.
Json example:
"loginContainer":[
{
"Comment":"",
"CommentUri":null,
"HttpOnly":true,
"Discard":false,
"Domain":"removed",
"Expired":false,
"Expires":"2026-07-02T12:42:43+02:00",
"Name":"locale",
"Path":"/",
"Port":"",
"Secure":false,
"TimeStamp":"2021-07-03T12:42:14.8645637+02:00",
"Value":"removed",
"Version":0
}
Collection to CookieContainer:
CookieContainer loginContainer = new CookieContainer();
foreach (Cookie cookie in root.LoginContainer)
{
Console.WriteLine(cookie.Name + " " + cookie.Value);
loginContainer.Add(cookie);
}
Request:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.CookieContainer = loginContainer;
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (MemoryStream ms = new MemoryStream())
{
response.GetResponseStream().CopyTo(ms);
listLabels.Add(ms.ToArray(), labels.Phrase);
}
Solved it by looping the CookieCollection and inserting inside the CookieContainer a new made Cookie with the CookieCollection values, not the one from the CookieCollection.
foreach (Cookie cookie in root.LoginContainer)
{
loginContainer.Add(new Cookie() { Name = cookie.Name, Value = cookie.Value, Path = cookie.Path, Domain = cookie.Domain });
}