3

I am writing an application to log into live.com. I have already successfully implemented this with the WebBrowser control, but I am now trying to do this with a headless browser. I would just use SimpleBrowser, but I need JavaScript support. So I am trying to do this by extending the WebClient class to support cookies. I thought originally that was my problem, but I did a simple test case with HttpWebRequest and HttpWebResponse objects to see how my cookies looked, and I was getting the same result.

The issue seems to be due to the "version=1" passed in the Set-Cookie response header for live.com. For my test case I ran the same code against twitter.com and login.live.com.

private void printCookies(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); ;
    CookieContainer cc = new CookieContainer();
    req.CookieContainer = cc;
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();            
    if (res.Cookies != null && res.Cookies.Count != 0)
    {
        Console.WriteLine("--------" + url + "--------");
        foreach (Cookie c in res.Cookies)
        {
            Console.WriteLine(c.ToString());
        }                
    }
    res.Close();            
}

Output:

--------https://login.live.com--------
$Version=1; MSPRequ=lt=1309969317&co=1&id=251248; $Path=/
--------http://twitter.com--------
k=209.43.1.25.1309969317382762
guest_id=v1%3A130996931741841563
auth_token= _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCC5%252BQQAxAToHaWQiJWNmZWM0ZTAyNmEyMWYx%250ANDg0MTM3YzJhZGRiZTljYmI2IgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--e08b33494bd0d4d688020d4c875f69a1192e2a84

If I look at the "Set-Cookie" value in the response headers of the respective URL's, this is what I see (grabbed from Fiddler):

login.live.com
Set-Cookie: MSPRequ=lt=1309969336&co=1&id=251248; path=/;version=1
Set-Cookie: MSPOK=$uuid-9f7c6cd2-5acc-497f-a634-079d78cb6e7f; domain=login.live.com;path=/;version=1

twitter.com
Set-Cookie: k=209.43.1.25.1309969337110139; path=/; expires=Wed, 13-Jul-11 16:22:17 GMT; domain=.twitter.com
Set-Cookie: guest_id=v1%3A130996933711260520; domain=.twitter.com; path=/; expires=Sat, 06 Jul 2013 04:22:17 GMT
Set-Cookie: auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCBrLQQAxAToHaWQiJTZhZTNjNmRmNjlhNWJl%250AMWEyMzkyZjNjNWQ4MjRmNDAxIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--2af4da7176dc0ee77c8379bc31a85c0301823e2d; domain=.twitter.com; path=/; HttpOnly

So login.live.com is grabbing only one of the cookies, and then storing the version and path separately. The twitter.com request is working as expected (not sure why there is a duplicate auth_token sent, but the CookieContainer handles it well).

Is this the expected behavior? It seems like IE, Firefox, and Chrome just ignore the "version=1" (from what I can tell in Fiddler). I could override the GetWebResponse method in my custom WebClient class to remove "version=1" from the response header's "Set-Cookie" value, but I was hoping there was a more obvious solution I am missing. It is possibly not the fault "version=1", but I don't see any other striking differences besides that between my two test cases.

Thanks,
Aaron Ray

Aaron Ray
  • 838
  • 5
  • 8
  • What is your reason for not using WebBrowser component since there seems to be rough operations (time consuming when handled with HttpWebRequest and HttpWebResponse )going on when connecting to live.com as it seems from fiddler ? – cgon Jul 08 '11 at 15:59
  • The WebBrowser object is a Forms control. It is essentially just a wrapper for MSHTML. I am wanting to implement a headless solution that doesn't worry about rendering HTML, and just grabs/sends the raw data. I am also trying to implement it in WPF, and I don't like the WPF WebBrowser as well as the Forms WebBrowser so I am running it in a Forms host. – Aaron Ray Jul 08 '11 at 17:14
  • The delay you see is probably due to the proxy on the HttpRequest.Proxy setting. Turning it off will show nothing in Fiddler but increase your connection speed. Regardless, my issue here is that this "should" work, but it isn't. There seems to be an inherent problem with the CookieContainer handling version, or a problem with my implementation of it. – Aaron Ray Jul 08 '11 at 17:17

1 Answers1

2

To remove the version part, you can use code like this:

foreach (Cookie c in cookieContainer.GetCookies(new Uri(uri)))
{
  c.Version = 0;
}
danmiser
  • 1,083
  • 12
  • 17
  • This removes the version, but it still truncates the cookie at the path. I need the line with the GUID, and the code is failing to get it. – Aaron Ray Jan 30 '12 at 15:29