Mr.*_*hoo 8 c# cookies httpwebrequest cookiecontainer http-headers
当我得到响应HttpWebRequest与HttpWebRequest.Headers.Add("Cookie",value)VS HttpWebRequest.CookieContainer,和结果的差异.
那么,它们之间有什么区别,何时使用它们.
根据我的经验,我在使用HttpWebRequest.CookieContainercookie管理时遇到了一些问题.它可以在一定程度上工作,但有时cookie不符合RFC,并且它们不会被CookieContainer正确解析.如果您通过将Cookie添加到请求的Cookie标头手动管理Cookie,那么对于某些不符合RFC的网站,您将获得更好的成功.其中一个问题CookieContainer是,如果日期中有逗号的日期,例如"2013年9月26日",它将完全解析为单独的cookie并破坏解析.另一个问题是,HTTP 302重定向设置的cookie 不会被重定向CookieContainer.
这取决于您对特定要求的最佳性能,但如果问题与CookieContainer手动设置cookie标题的结果不同,我建议您坚持手动设置cookie标题.希望Microsoft将来会更新它,以便我们可以回到使用漂亮的.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;
}
| 归档时间: | 
 | 
| 查看次数: | 7987 次 | 
| 最近记录: |