Question

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.

Was it helpful?

Solution

try:

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

%2C is like a comma :)

OTHER TIPS

Do this to set the cookie

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

and use Server.UrlDecode to get the original cookie value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top