I got this cookie returned from web-server in Set-Cookie header:
PHPSESSID=462rutd8g0sbg9sinh3hqa2ol6;
path=/,wordpress_test_cookie=WP+Cookie+check;
path=/,wordpress_ca07030412dd7fcd4dbeaae0bd9f5df9=testemail@example.com;
expires=Sun, 01-Jul-2012 05:11:02 GMT; path=/wp-content/plugins;
httponly,wordpress_ca07030412dd7fcd4dbeaae0bd9f5df9=testemail@example.com;
expires=Sun, 01-Jul-2012 05:11:02 GMT; path=/wp-admin;
httponly,wordpress_logged_in_ca07030412dd7fcd4dbeaae0bd9f5df9=testemail@example.com;
expires=Sun, 01-Jul-2012 05:11:02 GMT; path=/; httponly
I am trying to use CookieContainer.SetCookies(new Uri(url),cookie);
But it throws exception ‘{“An error occurred when parsing the Cookie header for Uri ‘http://www.smallbizads.us/‘.”} System.FormatException {System.Net.CookieException’
Some digging reveals that SetCookies uses , to break up the cookies. But that won’t work obviously because I have Expires header which also has ,
Any idea how can I get around this issue?
Note: I can only use this method by fetching cookie from Set-Cookie header due to WordPress issue. So please do not suggest any other option.
Took me a while to dissect it.
As far as I can see you want to set the PHPSESSID (PHP Session Id) generated by WordPress for one domain (.smallbizads.us) for several paths. The expiration date is always the same.
First you take your cookie header:
Split it by the ‘;’ delimiter character.
You wind up with a list of 12 items. For example:
The expiration date is shown multiple times in this list, but it always has the same value.
Just take the first expiration date and convert it to a DateTime.
OK, now let’s get the value for the cookie. The session id:
Now get the paths for which you need to set the cookie.
Now you can set the cookies using a CookieContainer and the System.Net.Cookie class.