gpt4 book ai didi

c# - CookieContainer 处理路径(谁吃了我的 cookie?)

转载 作者:太空狗 更新时间:2023-10-29 17:58:42 26 4
gpt4 key购买 nike

我正在从事一个涉及一些基本网络爬行的项目。我一直在非常成功地使用 HttpWebRequest 和 HttpWebResponse。对于 cookie 处理,我只有一个 CookieContainer,每次都分配给 HttpWebRequest.CookieContainer。我每次都会自动填充新的 cookie,不需要我进行额外处理。这一切都运行良好,直到不久前曾经运行的网站之一突然停止运行。我有理由相信这是 cookie 的问题,但我没有保留 cookie 从它开始工作时的记录,所以我不能 100% 确定。

我已经使用以下代码模拟了我所看到的问题:

CookieContainer cookieJar = new CookieContainer();

Uri uri1 = new Uri("http://www.somedomain.com/some/path/page1.html");
CookieCollection cookies1 = new CookieCollection();
cookies1.Add(new Cookie("NoPathCookie", "Page1Value"));
cookies1.Add(new Cookie("CookieWithPath", "Page1Value", "/some/path/"));

Uri uri2 = new Uri("http://www.somedomain.com/some/path/page2.html");
CookieCollection cookies2 = new CookieCollection();
cookies2.Add(new Cookie("NoPathCookie", "Page2Value"));
cookies2.Add(new Cookie("CookieWithPath", "Page2Value", "/some/path/"));

Uri uri3 = new Uri("http://www.somedomain.com/some/path/page3.html");

// Add the cookies from page1.html
cookieJar.Add(uri1, cookies1);

// Add the cookies from page2.html
cookieJar.Add(uri2, cookies2);

// We should now have 3 cookies
Console.WriteLine(string.Format("CookieJar contains {0} cookies", cookieJar.Count));

Console.WriteLine(string.Format("Cookies to send to page1.html: {0}", cookieJar.GetCookieHeader(uri1)));
Console.WriteLine(string.Format("Cookies to send to page2.html: {0}", cookieJar.GetCookieHeader(uri2)));
Console.WriteLine(string.Format("Cookies to send to page3.html: {0}", cookieJar.GetCookieHeader(uri3)));

这模拟了访问两个页面,这两个页面都设置了两个 cookie。然后它会检查这些 cookie 中的哪些将被设置到三个页面中的每一个。

在这两个 cookie 中,一个未指定路径而设置,另一个指定了路径。当未指定路径时,我曾假设 cookie 将被发送回该域中的任何页面,但它似乎只被发送回该特定页面。我现在假设这是正确的,因为它是一致的。

我的主要问题是处理带有指定路径的 cookie。当然,如果指定了路径,则应该将 cookie 发送到该路径中包含的任何页面。因此,在上面的代码中,“CookieWithPath”应该对/some/path/中的任何页面都有效,其中包括 page1.html、page2.html 和 page3.html。当然,如果您注释掉两个“NoPathCookie”实例,那么“CookieWithPath”会像我预期的那样发送到所有三个页面。但是,如上包含“NoPathCookie”后,“CookieWithPath”只会发送到 page2.html 和 page3.html,而不是 page1.html。

为什么会这样,是否正确?

在搜索此问题时,我遇到了有关 CookieContainer 中域处理问题的讨论,但未能找到有关路径处理的任何讨论。

我正在使用 Visual Studio 2005/.NET 2.0

最佳答案

When a path is not specified, I had assumed that the cookie would be sent back to any page in that domain, but it seems to only get sent back to that specific page. I'm now assuming that is correct as it is consistent.

是的,没错。只要未指定域或路径,就会从当前 URI 中获取。

OK,我们来看一下CookieContainer。有问题的方法是 InternalGetCookies(Uri) .这是有趣的部分:

while (enumerator2.MoveNext())
{
DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator2.get_Current();
string text2 = (string)dictionaryEntry.get_Key();
if (!uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(text2)))
{
if (flag2)
{
break;
}
else
{
continue;
}
}
flag2 = true;
CookieCollection cookieCollection2 = (CookieCollection)dictionaryEntry.get_Value();
cookieCollection2.TimeStamp(CookieCollection.Stamp.Set);
this.MergeUpdateCollections(cookieCollection, cookieCollection2, port, flag, i < 0);
if (!(text2 == "/"))
{
continue;
}
flag3 = true;
continue;
}

enumerator2这是一个(排序的)cookie 路径列表。它以这样一种方式排序,更具体的路径(如 /directory/subdirectory/ )在不太具体的路径(如 /directory/ )之前,否则 - 按字典顺序( /directory/page1/directory/page2 之前)。

该代码实际上执行以下操作:它遍历此 cookie 路径列表,直到找到第一条路径,即请求的 URI 路径的前缀。然后它在该路径下添加一个 cookie 到输出并设置 flag2true ,这意味着“好的,我终于在列表中找到了与请求的 URI 实际相关的位置”。之后,第一个遇到的路径(不是所请求 URI 路径的前缀)被认为是相关路径的结尾,因此代码通过执行 break 停止搜索 cookie。 .

显然,这是一种防止扫描整个列表的优化,如果没有路径指向具体页面,它显然有效。现在,对于您的情况,路径列表如下所示:

/some/path/page1.html
/some/path/page2.html
/some/path/

您可以使用调试器进行检查,查找 ((System.Net.PathList)(cookieJar.m_domainTable["www.somedomain.com"])).m_list在监 window 口中

因此,对于“page1.html”URI,代码在 page2.html 处中断项目,也没有机会处理 /some/path/项目。

总而言之:这显然是 CookieContainer 中的另一个错误。我相信它应该在连接上报告。

PS:每个类(class)的 bug 太多了。我只希望 MS 为此类编写测试的那个人已经被解雇了。

关于c# - CookieContainer 处理路径(谁吃了我的 cookie?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com