8

HttpWebRequest当我从with HttpWebRequest.Headers.Add("Cookie",value)vs得到响应时HttpWebRequest.CookieContainer,结果是不同的。

那么,它们之间有什么区别,以及何时使用它们。

4

1 回答 1

8

根据我的经验,我在使用HttpWebRequest.CookieContainercookie 管理时遇到了一些问题。它可以在一定程度上起作用,但有时 cookie 不符合 RFC,并且它们没有被CookieContainer正确解析。如果您通过将 cookie 添加到请求的 Cookie 标头来手动管理 cookie,那么您将在某些不符合 RFC 的网站上获得更好的成功。其中一个问题CookieContainer是,如果日期中有一个带有逗号的日期,例如“2013 年 9 月 26 日”,它将完全将其解析为单独的 cookie 并中断解析。另一个问题是在HTTP 302重定向上设置的 cookie 不会被CookieContainer.

什么是最适合您的特定要求的取决于您,但如果问题CookieContainer提供的结果与手动设置 cookie 标头不同,我建议您坚持手动设置 cookie 标头。希望微软将来会更新它,以便我们可以重新使用漂亮的 .NET 类来管理 cookie。

编辑:
我遇到了一些正确解析“Set-Cookie”标头的代码。它处理以逗号分隔的 cookie 并提取每个 cookie 的名称、到期时间、路径、值和域。

这段代码比微软自己的 cookie 解析器工作得更好,这确实是官方 cookie 解析器应该做的。我不知道为什么微软还没有解决这个问题,因为这是一个非常常见的问题。

这是原始代码: http ://snipplr.com/view/4427/

我在这里发布它以防链接在某些时候断开:

public static CookieCollection GetAllCookiesFromHeader(string strHeader, string strHost)
{
    ArrayList al = new ArrayList();
    CookieCollection cc = new CookieCollection();
    if (strHeader != string.Empty)
    {
        al = ConvertCookieHeaderToArrayList(strHeader);
        cc = ConvertCookieArraysToCookieCollection(al, strHost);
    }
    return cc;
}


private static ArrayList ConvertCookieHeaderToArrayList(string strCookHeader)
{
    strCookHeader = strCookHeader.Replace("\r", "");
    strCookHeader = strCookHeader.Replace("\n", "");
    string[] strCookTemp = strCookHeader.Split(',');
    ArrayList al = new ArrayList();
    int i = 0;
    int n = strCookTemp.Length;
    while (i < n)
    {
        if (strCookTemp[i].IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0)
        {
            al.Add(strCookTemp[i] + "," + strCookTemp[i + 1]);
            i = i + 1;
        }
        else
        {
            al.Add(strCookTemp[i]);
        }
        i = i + 1;
    }
    return al;
}


private static CookieCollection ConvertCookieArraysToCookieCollection(ArrayList al, string strHost)
{
    CookieCollection cc = new CookieCollection();

    int alcount = al.Count;
    string strEachCook;
    string[] strEachCookParts;
    for (int i = 0; i < alcount; i++)
    {
        strEachCook = al[i].ToString();
        strEachCookParts = strEachCook.Split(';');
        int intEachCookPartsCount = strEachCookParts.Length;
        string strCNameAndCValue = string.Empty;
        string strPNameAndPValue = string.Empty;
        string strDNameAndDValue = string.Empty;
        string[] NameValuePairTemp;
        Cookie cookTemp = new Cookie();

        for (int j = 0; j < intEachCookPartsCount; j++)
        {
            if (j == 0)
            {
                strCNameAndCValue = strEachCookParts[j];
                if (strCNameAndCValue != string.Empty)
                {
                    int firstEqual = strCNameAndCValue.IndexOf("=");
                    string firstName = strCNameAndCValue.Substring(0, firstEqual);
                    string allValue = strCNameAndCValue.Substring(firstEqual + 1, strCNameAndCValue.Length - (firstEqual + 1));
                    cookTemp.Name = firstName;
                    cookTemp.Value = allValue;
                }
                continue;
            }
            if (strEachCookParts[j].IndexOf("path", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                strPNameAndPValue = strEachCookParts[j];
                if (strPNameAndPValue != string.Empty)
                {
                    NameValuePairTemp = strPNameAndPValue.Split('=');
                    if (NameValuePairTemp[1] != string.Empty)
                    {
                        cookTemp.Path = NameValuePairTemp[1];
                    }
                    else
                    {
                        cookTemp.Path = "/";
                    }
                }
                continue;
            }

            if (strEachCookParts[j].IndexOf("domain", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                strPNameAndPValue = strEachCookParts[j];
                if (strPNameAndPValue != string.Empty)
                {
                    NameValuePairTemp = strPNameAndPValue.Split('=');

                    if (NameValuePairTemp[1] != string.Empty)
                    {
                        cookTemp.Domain = NameValuePairTemp[1];
                    }
                    else
                    {
                        cookTemp.Domain = strHost;
                    }
                }
                continue;
            }
        }

        if (cookTemp.Path == string.Empty)
        {
            cookTemp.Path = "/";
        }
        if (cookTemp.Domain == string.Empty)
        {
            cookTemp.Domain = strHost;
        }
        cc.Add(cookTemp);
    }
    return cc;
}
于 2013-09-26T20:04:51.537 回答