CookieContainer containing commas workaround

  • Thread starter Thread starter cmklar
  • Start date Start date
C

cmklar

I'm working on a spider, which has to crawl a certain site, one of the
cookie they are sending down contains some commas in the cookie value
part, and this seems to make trouble while coding in dotnet.

I'm having difficulties in both directions; when getting the cookie
down dotnet splits it to multiple cookies (as if the comma would be the
cookie delimiter).

and when trying to send back up such a cookie it will throw an
exception on the following line:
Request.CookieContainer.Add(new Cookie("A","some,commas","/",
"www.mysite.com"));

Exception="The 'Value'=some,commas' part of the Cookie is invalid."

Regardless of what the RFC dictates if this site is using it and it
works on all major browsers (tried IE & FireFox) there should be a way
to do it in dotnet.

so on the way down i overcame it by manually parsing the "Set-Cookie"
header.

but I'm lost on how to send it back up!!

trying to set the Request.Headers["Cookie"] troughs an exception.
setting the Request.CookieContainer also throws an exception.

so how can i attach this value??
 
Thus wrote (e-mail address removed),
I'm working on a spider, which has to crawl a certain site, one of the
cookie they are sending down contains some commas in the cookie value
part, and this seems to make trouble while coding in dotnet.

I'm having difficulties in both directions; when getting the cookie
down dotnet splits it to multiple cookies (as if the comma would be
the cookie delimiter).

and when trying to send back up such a cookie it will throw an
exception on the following line: Request.CookieContainer.Add(new
Cookie("A","some,commas","/", "www.mysite.com"));

Exception="The 'Value'=some,commas' part of the Cookie is invalid."

Regardless of what the RFC dictates if this site is using it and it
works on all major browsers (tried IE & FireFox) there should be a way
to do it in dotnet.

That's a pretty questionable way to deal with standards. But anyway, Netscape's
cookie spec also allows to send forbidden characters in URL encoded format,
and that works with CookieContainer as well.

string val = HttpUtility.UrlEncode("foo,bar");
request.CookieContainer.Add(new Cookie("foo", val, "/", "www.somewhere.org"));

Cheers,
 
Thanks, I'll give it a try.

Joerg said:
Thus wrote (e-mail address removed),


That's a pretty questionable way to deal with standards. But anyway, Netscape's
cookie spec also allows to send forbidden characters in URL encoded format,
and that works with CookieContainer as well.

string val = HttpUtility.UrlEncode("foo,bar");
request.CookieContainer.Add(new Cookie("foo", val, "/", "www.somewhere.org"));

Cheers,
 
Back
Top