.net HttpWebRequestCookieContainer

.net HttpWebRequestCookieContainer,.net,httpwebrequest,path,cookiecontainer,.net,Httpwebrequest,Path,Cookiecontainer,如何处理路径不是“/”的Cookie。 HttpWebRequest对象返回以下标头: HTTP/1.1 302 Moved Temporarily Transfer-Encoding: chunked Date: Wed, 10 Jun 2009 13:22:53 GMT Content-Type: text/html; charset=UTF-8 Expires: Wed, 10 Jun 2009 13:22:53 GMT Cache-Control: no-cache, must-reva

如何处理路径不是“/”的Cookie。 HttpWebRequest对象返回以下标头:

HTTP/1.1 302 Moved Temporarily
Transfer-Encoding: chunked
Date: Wed, 10 Jun 2009 13:22:53 GMT
Content-Type: text/html; charset=UTF-8
Expires: Wed, 10 Jun 2009 13:22:53 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Server: nginx/0.7.41
X-Powered-By: PHP/5.2.9
Last-Modified: Wed, 10 Jun 2009 13:22:52 GMT
Pragma: no-cache
Set-Cookie: cookie1=c1; path=/; domain=site.com
Set-Cookie: cookie2=c2; path=/content; domain=site.com; httponly
Set-Cookie: cookie3=c3; path=/admin; domain=site.com; httponly
Set-Cookie: cookie4=c4; path=/; domain=site.com; httponly
Location: http://site.com/admin/
Via: 1.1 mvo-netcache-02 (NetCache NetApp/6.0.7)
在cookie集合中迭代只包含路径为“/”的cookie。 所以cookiecontainer中只有cookie1和cookie4

为什么其余的没有被收集? 如何使用“/”以外的路径访问cookie?我能把它们都收集起来吗 在一个容器里


谢谢

鉴于此问题在网上出现的频率很高,我怀疑问题在于.NET库代码不支持多个Set-Cookie头(无论是始终还是仅在某些情况下)。不管怎样,这很容易解决。只需直接从设置的Cookie头中提取Cookie。下面是一些代码(最初是从附加到的代码中复制的),它显示了如何直接从Set Cookie头中提取Cookie

    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;
    }
公共静态CookieCollection GetAllCookiesFromHeader(字符串strHeader,字符串strHost)
{
ArrayList al=新的ArrayList();
CookieCollection cc=新的CookieCollection();
if(strHeader!=string.Empty)
{
al=转换器CookieHeaderToarRayList(strHeader);
cc=转换器CookieArrayStocokieCollection(al,strHost);
}
返回cc;
}
私有静态数组列表转换器CookieHeaderToarRayList(字符串strCookHeader)
{
strCookHeader=strCookHeader.Replace(“\r”,”);
strCookHeader=strCookHeader.Replace(“\n”,”);
字符串[]strCookTemp=strCookHeader.Split(',');
ArrayList al=新的ArrayList();
int i=0;
int n=结构周期长度;
而(i0)
{
加上(strCookTemp[i]+”,“+strCookTemp[i+1]);
i=i+1;
}
其他的
{
新增(strCookTemp[i]);
}
i=i+1;
}
返回al;
}
私有静态CookieCollection转换器CookieArrayStoCokieCollection(ArrayList al,string strHost)
{
CookieCollection cc=新的CookieCollection();
int alcount=总计数;
串串;
字符串[]链表部件;
for(int i=0;i=0)
{
strpname和pvalue=strEachCookParts[j];
if(strpname和pvalue!=string.Empty)
{
NameValuePairTemp=strPNameAndPValue.Split('=');
if(NameValuePairTemp[1]!=string.Empty)
{
cookTemp.Path=NameValuePairTemp[1];
}
其他的
{
cookTemp.Path=“/”;
}
}
继续;
}
if(strEachCookParts[j].IndexOf(“域”,StringComparison.OrdinalIgnoreCase)>=0)
{
strpname和pvalue=strEachCookParts[j];
if(strpname和pvalue!=string.Empty)
{
NameValuePairTemp=strPNameAndPValue.Split('=');
if(NameValuePairTemp[1]!=string.Empty)
{
cookTemp.Domain=NameValuePairTemp[1];
}
其他的
{
cookTemp.Domain=strHost;
}
}
继续;
}                    
}
if(cookTemp.Path==string.Empty)
{
cookTemp.Path=“/”;
}
if(cookTemp.Domain==string.Empty)
{
cookTemp.Domain=strHost;
}
抄送添加(cookTemp);
}
返回cc;
}

实际上CookieContainer得到了所有的cookies,但你看不到它们。当您从GelCookies()方法获得它时,它将根据当前路径和域为您提供合适的Cookie

CookieContainer将处理域、路径以及到期,但它在子域处理中有一个bug

检查此项以了解详细信息和错误修复:

bryce,我的回答帮你解决了这个问题吗?