2

I'm trying to send a cookie with a request to an Asp.net page. From what I understand it expects the cookies value to NOT be encoded. If I encode the value it doesn't register it.

CookieContainer won't let me add the non encoded value to it though. I can't seem to find a work around...

My code is essentially

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(@"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, cookieValue, "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;

This throws a CookieException saying "The 'Value'='version=1&Type=a,b,c,d,e,f' part of the cookie is invalid."

James
  • 4,146
  • 1
  • 20
  • 35

1 Answers1

0

You could try UrlEncoding value.

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(@"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, Server.UrlEncode(cookieValue), "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • I have tried `HttpUtility.UrlEncode` which doesn't work as apparently in asp.net you don't unencode them (see http://stackoverflow.com/questions/1136405/handling-a-comma-inside-a-cookie-value-using-nets-c-system-net-cookie). – James Jul 15 '14 at 11:47