0

I need to put 2 cookies in a webclient with specific names and values so not just a value and I have got the following code to add a cookie to the header with a value but I can't find a way to specify the name of the cookie

System.Net.WebClient HR = new System.Net.WebClient();
HR.Headers.Add(System.Net.HttpRequestHeader.Cookie, "Cookie1 value");
HR.Headers.Add(System.Net.HttpRequestHeader.Cookie, "Cookie2 value");
DFSFOT
  • 536
  • 3
  • 19

1 Answers1

0

Try this:

HttpCookie cookie = new HttpCookie("The Name I Wish"); // Create a cookie and give it a name
cookie.Expires = DateTime.Now.AddDays(30);       // expries in one month
cookie.Value = "Some Value";                          // set value
HttpContext.Response.Cookies.Add(cookie); 

HttpCookie cookie1 = new HttpCookie("The Other unique Name I Wish"); // Create a cookie and give it a name
cookie1.Expires = DateTime.Now.AddDays(20);       // expries in 20 days
cookie1.Value = "Some other value Value";                          // set value
HttpContext.Response.Cookies.Add(cookie1);

For Sending Cookies by name via WebClient you can do it by writing you own custom format as follows:

wb.Headers.Add(HttpRequestHeader.Cookie, "cookiename=cookievalue"); 

For multiple cookies by name:

wb.Headers.Add(HttpRequestHeader.Cookie, 
          "cookiename1=cookievalue1;" +
          "cookiename2=cookievalue2");

Original answer is in here: Multi Cookies Using WebClient

Community
  • 1
  • 1
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45