- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在从事一个涉及一些基本网络爬行的项目。我一直在非常成功地使用 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 到输出并设置 flag2
至 true
,这意味着“好的,我终于在列表中找到了与请求的 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/
在我的主要组件中,我有: mounted() { window.$cookie.set('cookie_name', userName, expiringTime); }, 这会产生以下错误:
我正在学习 cookie,并且我想知道在编写依赖 cookie 来存储状态的 Web 应用程序时浏览器的支持情况。 对于每个域/网站,可以向浏览器发送多少个 Cookie,大小是多少? 如果发送并存储
我已经为我的站点设置了一个 cdn,并将其用于 css、js 和图像。 网站只提供那些文件 我的问题是 firefox 中的页面速度插件对于我的图片请求,我看到了一个 cookie Cookie fc
在阅读了 Internet 上的文档和帖子后,我仍然无法解决 jMeter 中的 Cookie Manager 问题。 我在响应头中得到了 sid ID,但它没有存储在我的 cookie 管理器中。
我正在 Node.JS 中处理一些类似浏览器的 cookie 处理,想知道从 NodeJS and HTTP Client - Are cookies supported? 开始对这段代码进行扩展到什
我正在此堆栈上构建自托管 Web 服务器:欧文南希网络 API 2 我正在使用 Katana 的 Microsoft.Owin.Security.Cookies 进行类似表单的身份验证。我得到了 Se
我有一个从另一个网站加载资源的网站。我已经能够确定: 第三方网站在用户的浏览器上放置 cookie。 如果我在浏览器设置中禁用第三方 cookie,第三方网站将无法再在浏览器上放置 cookie。 该
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我正在使用 python mechanize 制作登录脚本。我已经读到 Mechanize 的 Browser() 对象将自动处理 cookie 以供进一步请求。 我怎样才能使这个 cookie 持久
我正在尝试在 www.example.com 和 admin.other.example.com 之间共享 cookie 我已经能够使其与 other.example.com 一起使用,但是无法访问子
我设置了一个域为 .example.com 的 cookie .它适用于我网站上的每个一级子域,应该如此。 但是,它不适用于 n 级子域,即 sub.subdomain.example.com和 to
我想让用户尽可能长时间地登录。 我应该使用什么? 普通 cookies 持久性 cookie 快闪 cookies ip地址 session 或这些的某种组合? 最佳答案 我认为 Flash cook
如果给定的 Web 服务器只能读取其域内设置的 cookie,那么 Internet 广告商如何从其网络外的网站跟踪用户的 Web 流量? 是否存在某种“supercookie”全局广告系统,允许广告
我知道一个 cookie 可以容纳多少数据是有限制的,但是我们可以设置多少个 cookie 有限制吗? 最佳答案 来自 http://www.ietf.org/rfc/rfc2109.txt Prac
如果我拒绝创建 cookie,则在我的浏览器中创建名称为 __utma、__utmb 等的 cookie。我认为这个 cookie 是用于谷歌分析的。任何人都知道谷歌如何创建这个 cookie,即使浏
我有一个生产环境和一个登台环境。我想知道我是否可以在环境之间沙箱 cookie。我的设置看起来像 生产 domain.com - 前端 SPA api.domain.com - 后端节点 分期 sta
我想知道浏览器(即 Firefox )和网站的交互。 当我将用户名和密码提交到登录表单时,会发生什么? 我认为该网站向我发送了一些 cookie,并通过检查这些 cookie 来授权我。 cookie
我在两个不同的域中有两个网络应用程序 WebApp1 和 WebApp2。 我在 HttpResponse 的 WebApp1 中设置 cookie。 如何从 WebApp2 中的 HttpReque
我正在使用Dartium“Version 34.0.1847.0 aura(264987)”,并从Dart创建一个websocket。但是,如果不是httpOnly,我的安全 session cook
我从 Headfirst Javascript 书中获取了用于 cookie 的代码。但由于某种原因,它不适用于我的浏览器。我主要使用chrome和ff,并且我在chrome中启用了本地cookie。
我是一名优秀的程序员,十分优秀!