I have implemented this code to request a page and read the cookies set by the server:
myURI = new Uri(strURI);
request = (HttpWebRequest)HttpWebRequest.Create(myURI);
request.CookieContainer = new CookieContainer();
request.CookieContainer.GetCookies(myURI);
using (var response = (HttpWebResponse)request.GetResponse())
{
foreach (Cookie cook in response.Cookies)
{
Console.WriteLine(" Cookie:");
Console.WriteLine($"{cook.Name} = {cook.Value}");
Console.WriteLine($"Domain: {cook.Domain}");
Console.WriteLine($"Path: {cook.Path}");
Console.WriteLine($"Port: {cook.Port}");
Console.WriteLine($"Secure: {cook.Secure}");
Console.WriteLine($"When issued: {cook.TimeStamp}");
}
}
Is there a way to create new cookies and send it to the browser cookies, so that the server can read them?
You can use HttpClientHandler with CookieContainer to send requests with cookies.
For Post your request with cookies
And for reading cookies if you only have access to HttpResponseMessage You can read it from headers