httpwebrequest Cookie 容器 [英] httpwebrequest Cookiecontainer

查看:26
本文介绍了httpwebrequest Cookie 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理路径不是/"的 cookie.HttpWebRequest 对象返回这些标头:

How do I handle cookies with paths other than "/". An HttpWebRequest object returns these headers:

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.

Iterating through a cookie collection only containsthe cookies with a path of "/". So the cookiecontainer only has cookie1 and cookie4 in it.

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

Why are the rest not being collected? How do I access the cookies with paths other than "/"? Can I gather them all in one container?

谢谢

推荐答案

鉴于此问题在网上出现的频率,我怀疑问题在于 .NET 库代码不支持多个 Set-Cookie 标头(或者所有时间或仅在某些情况下).无论如何,它很容易解决.只需直接从 Set-Cookie 标头中提取 cookie.这是一些代码(最初是从附加到 thisthread),它展示了如何直接从 Set-Cookie 标头中提取 cookie.

Given how frequently this issue comes up online, I suspect the problem is that the .NET library code doesn't support multiple Set-Cookie headers (either all the time or only under some circumstances). Regardless, it's pretty easy to work around. Just extract the cookies directly from the Set-Cookie headers. Here's some code (originally copied from code attached to this thread) which shows how to extract cookies directly from the Set-Cookie header.

    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("
", "");
        strCookHeader = strCookHeader.Replace("
", "");
        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;
    }

这篇关于httpwebrequest Cookie 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆