如何自WebResponse得到多个设置Cookie?(How to get multiple Se

2019-09-22 06:31发布

我想接收并存储到CookieContainer多个cookie。 服务器一个请求给我(我看到这行到提琴手)中发送此代码:

设置Cookie:ASP.NET_SessionId = fjkfjrfjeijrfjrifj; 路径= /; 仅Http

设置Cookie:鲁阿-USM =日期=二零一二年十月九日&p-型= 1& TS = 11&ID = cvvkmrfmpr44r4rjj; 期满=星期五,31日 - 12月9999 23点59分59秒GMT; 路径= /

设置Cookie:鲁阿-USM2 =日期=二零一二年十月九日&p-型= 1& TS = 11; 期满=星期五,31日 - 12月9999 23点59分59秒GMT; 路径= /

设置Cookie:VisitorDone = 1; 期满=星期五,31日 - 12月9999 23点59分59秒GMT; 路径= /

当你看到有4块饼干。 但是,当我读到response.Cookies.Count我得到的只有一个:* ASP.NET_SessionId *。

我用下面的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

我试过手动解析的Set-Cookie字段:

string cookiesText = (response as HttpWebResponse).Headers[HttpResponseHeader.SetCookie];
if (!String.IsNullOrEmpty(cookiesText))
{
    CookieCollection cookiesList = GetAllCookiesFromHeader(cookiesText, host);
    if (cookiesList != null)
        _cookieContainer.Add(new Uri(host),cookiesList);
}


    private 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 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 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;

                        Encoding iso = Encoding.GetEncoding("utf-8");//may be utf-8
                        allValue = HttpUtility.UrlEncode(allValue, iso);

                        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;
    }

但得到的错误:

“该‘域’=‘HTTP://xxxx.xxxx’cookie的部分是无效的。”

据我了解一些相关的不非标准字符转换成的Set-Cookie值。 但我通过编码的所有值HttpUtility.UrlEncode

Answer 1:

问题是与CookieCollection cookiesList = GetAllCookiesFromHeader(cookiesText, host); 线。 host变量不能包含“HTTP://”字符串。



Answer 2:

您可能还需要另一个IF语句添加到ConvertCookieArraysToCookieCollection方法来获取到期日为好。 以下应为你工作。

                if (strEachCookParts[j].IndexOf("expires=", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    strPNameAndPValue = strEachCookParts[j];
                    if (strPNameAndPValue != string.Empty)
                    {
                        NameValuePairTemp = strPNameAndPValue.Split('=');
                        if (NameValuePairTemp[1] != string.Empty)
                        {
                            cookTemp.Expires = Convert.ToDateTime(NameValuePairTemp[1]);
                        }
                    }
                    continue;
                }


文章来源: How to get multiple Set-Cookie from WebResponse?