的CookieContainer错误? [英] CookieContainer bug?
问题描述
我很困惑的CookieContainer如何处理域,所以我创建此测试。
该测试显示的CookieContainer不返回任何的Cookie为example.com,但根据RFC它应该返回至少2饼干。
是不是一个错误?
如何使它工作?
下面是关于这个bug的讨论:
的http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c4edc965-2dc2-4724-8f08-68815cf1dce6
<%@页面语言=C#%><%@导入命名空间=System.Net%GT;
!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><脚本=服务器>
的CookieContainer getContainer()
{
结果的CookieContainer =新的CookieContainer(); 开放的我们的uri =新的URI(http://sub.example.com);
字符串cookieH = @的Test1 = VAL;域= sub.example.com;路径= /;
result.SetCookies(URI,cookieH); cookieH = @的Test2 = VAL;域= .example.com的,PATH = /;
result.SetCookies(URI,cookieH); cookieH = @Test3的= VAL;域= example.com;路径= /;
result.SetCookies(URI,cookieH); 返回结果;
} 无效测试()
{
的CookieContainer饼干= getContainer();
lblResult.Text + =< BR>总饼干数:+ cookie.Count +&放大器; NBSP;&安培; NBSP;预计:3; 开放的我们的uri =新的URI(http://sub.example.com);
CookieCollection COLL = cookie.GetCookies(URI);
lblResult.Text + =< BR>作为+ URI +曲奇计数:+ coll.Count +&放大器; NBSP;&安培; NBSP;预计:2; URI =新的URI(http://other.example.com);
科尔= cookie.GetCookies(URI);
lblResult.Text + =< BR>作为+ URI +曲奇计数:+ coll.Count +&放大器; NBSP;&安培; NBSP;预计:2; URI =新的URI(http://example.com);
科尔= cookie.GetCookies(URI);
lblResult.Text + =< BR>作为+ URI +曲奇计数:+ coll.Count +&放大器; NBSP;&安培; NBSP;预计:2; } 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
测试();
}
< / SCRIPT>< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>的CookieContainer测试页< /标题>
< /头>
<身体GT;
<表ID =frmTest=服务器>
< ASP:标签ID =lblResult的EnableViewState =假=服务器>< / ASP:标签>
< /表及GT;
< /身体GT;
< / HTML>
我刚刚发现了这个bug修复和这里讨论:
的http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
下面是解决方案:
- 请不要使用。新增(饼干),只能使用。新增(URI,饼干)方法。
-
通话BugFix_CookieDomain每次一个cookie添加到容器中或
在使用之前.GetCookie或之前系统中使用的容器。私人无效BugFix_CookieDomain(的CookieContainer的CookieContainer)
{
System.Type的_ContainerType = typeof运算(的CookieContainer);
哈希表的表=(哈希表)_ContainerType.InvokeMember(m_domainTable
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance,
空值,
的CookieContainer,
新的对象[] {});
ArrayList的键=新的ArrayList(table.Keys);
的foreach(在钥匙串keyObj)
{
字符串键=(keyObj作为字符串);
如果(键[0] =='。')
{
字符串则newkey = key.Remove(0,1);
表[则newkey] =表[keyObj]
}
}
}
I'm confused how CookieContainer handles domain, so I create this test. This test shows cookieContainer doesn't return any cookie for "example.com" but according to RFC it should return at least 2 cookies.
Isn't it a bug?
How make it to work?
Here is a discussion about this bug:
http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c4edc965-2dc2-4724-8f08-68815cf1dce6
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
CookieContainer getContainer()
{
CookieContainer result = new CookieContainer();
Uri uri = new Uri("http://sub.example.com");
string cookieH = @"Test1=val; domain=sub.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test2=val; domain=.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test3=val; domain=example.com; path=/";
result.SetCookies(uri, cookieH);
return result;
}
void Test()
{
CookieContainer cookie = getContainer();
lblResult.Text += "<br>Total cookies count: " + cookie.Count + " expected: 3";
Uri uri = new Uri("http://sub.example.com");
CookieCollection coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://other.example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
}
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CookieContainer Test Page</title>
</head>
<body>
<form id="frmTest" runat="server">
<asp:Label ID="lblResult" EnableViewState="false" runat="server"></asp:Label>
</form>
</body>
</html>
I just found the fix for this bug and discussed here: http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
Here is the solution:
- Don't use .Add(Cookie), Use only .Add(Uri, Cookie) method.
Call BugFix_CookieDomain each time you add a cookie to the container or before you use .GetCookie or before system use the container.
private void BugFix_CookieDomain(CookieContainer cookieContainer) { System.Type _ContainerType = typeof(CookieContainer); Hashtable table = (Hashtable)_ContainerType.InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { }); ArrayList keys = new ArrayList(table.Keys); foreach (string keyObj in keys) { string key = (keyObj as string); if (key[0] == '.') { string newKey = key.Remove(0, 1); table[newKey] = table[keyObj]; } } }
这篇关于的CookieContainer错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!