开发者

Unexpected behavior in CookieContainer object when version is specified in response Set-Cookie C# .NET 2.0-4.0

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();开发者_StackOverflow社区
    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


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

foreach (Cookie c in cookieContainer.GetCookies(new Uri(uri)))
{
  c.Version = 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜