C
C
CityzenUNDEAD2020-11-14 21:21:12
Parsing
CityzenUNDEAD, 2020-11-14 21:21:12

How to convert the data received in the form of cookie text to the Cookie type?

Hello everyone!
I get cookie data from the request like this

System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                System.Net.Http.HttpResponseMessage response = await client.GetAsync("https://www.igromania.ru/");
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                System.Net.Http.Headers.HttpResponseHeaders hd = response.Headers;
                System.Net.CookieContainer cookieBox = new System.Net.CookieContainer();

                if (hd.Contains("Set-Cookie"))
                {
                    var setCookieHeaders = hd.GetValues("Set-Cookie");
                    foreach(var cooks in setCookieHeaders) 
                    {
                        cookieBox.Add();
                        Console.WriteLine(cooks);
                    }


That is, I sort through the received cookies from setCookieHeaders. Inside this enumeration, I want to write all cookies to the cookieBox variable, but in order to write them there, they must be of the Cookie type, and plain text data is not converted to the Cookie class.
What can be done here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-11-14
@vabka

The question was born!
Let's look at MDN again .

foreach(var cookieLine insetCookieHeaders)
{
 // Допустим, что нам нужна только первая часть куки, без всяких HttpOnly и Expires
  var cookie = cookieLine.Split(';')[0];
  cookieBox.Add(cookie); // Хз, что за cookieBox. Предполагаю, что это List<string>
  Console.WriteLine(cookie);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question