在响应 Set-Cookie C# .NET 2.0-4.0 中指定版本时,CookieContainer 对象中出现意外行为
我正在编写一个应用程序来登录 live.com。我已经使用 WebBrowser 控件成功实现了这一点,但我现在尝试使用无头浏览器来实现这一点。我只想使用 SimpleBrowser,但我需要 JavaScript 支持。所以我试图通过扩展 WebClient 类来支持 cookie 来做到这一点。我最初认为这是我的问题,但我使用 HttpWebRequest 和 HttpWebResponse 对象做了一个简单的测试用例来查看我的 cookie 的外观,并且得到了相同的结果。
该问题似乎是由于 live.com 的 Set-Cookie 响应标头中传递的“version=1”造成的。对于我的测试用例,我针对 twitter.com 和 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();
}
输出:
--------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
如果我查看相应 URL 的响应标头中的“Set-Cookie”值,这就是我看到的内容(从 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
因此,login.live.com 仅抓取其中一个 cookie,然后存储分别指定版本和路径。 twitter.com 请求按预期工作(不确定为什么发送了重复的 auth_token,但 CookieContainer 处理得很好)。
这是预期的行为吗?看起来 IE、Firefox 和 Chrome 只是忽略了“version=1”(据我在 Fiddler 中所知)。我可以重写自定义 WebClient 类中的 GetWebResponse 方法,以从响应标头的“Set-Cookie”值中删除“version=1”,但我希望有一个我缺少的更明显的解决方案。这可能不是错误“version=1”,但除了两个测试用例之间的差异之外,我没有看到任何其他显着差异。
谢谢,
亚伦·雷
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要删除版本部分,您可以使用如下代码:
To remove the version part, you can use code like this: