.net 框架有个 BUG ,根据域名取 cookie 的时候有些域名是取不到的,好像是他们判断域名的条件那里有问题,这个是底层的问题,要做一点小操作,然后再按域名取就可以全部取到了
/// <summary>
/// this is a hackfix for microsoft bug, where cookies are not shared between
www.domain.com and
domain.com /// </summary>
/// <param name="cc"></param>
public static void ImproveCookieContainer(ref CookieContainer cc)
{
Hashtable table = (Hashtable)cc.GetType().InvokeMember(
"m_domainTable",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance,
null, cc, new object[] { });
var comparerPreperty = table.GetType().GetField("_keycomparer",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance);
if (comparerPreperty != null)
{
comparerPreperty.SetValue(table, new DomainComparer());
}
}