Search code examples
c#cookiecontainer

CookieContainer manual cookie override


Hello i'm having slight problem when setting one cookie. I am using HttpWebRequest class to send my requests. And this code to set cookie i need:

    CookieContainer myContainer = new CookieContainer();
    myContainer.Add(new Uri("address"), new Cookie("cookie", "val1,val2"));

But that throws error

The 'Value'='val1,val2' part of the cookie is invalid.

I have also tried UrlEncoding cookie like:

    new Cookie("cookie", HttpUtility.UrlEncode("val1,val2"))

But cookie turns into Cookie: cookie=val1%2cval2 which is rejected by webpage.

I have also tried using quotes around the value part:

    new Cookie("cookie", "\"val1,val2\""))

But this one is also rejected by website.

Maybe anyone know a way how i could manually override cookie value to what i need without triggering error?

Help would be appreciated.


Solution

  • try:

    new Cookie("cookie", HttpUtility.UrlEncode("val1%2Cval2"))
    

    %2C is like a comma :)