首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从WebResponse获得多套曲奇?

如何从WebResponse获得多套曲奇?
EN

Stack Overflow用户
提问于 2012-09-09 23:21:53
回答 2查看 5.5K关注 0票数 3

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

Set: ASP.NET_SessionId=fjkfjrfjeijrfjrifj;path=/;HttpOnly Set-Cookie: rua-usm=date=10.09.2012&ptype=1&ts=11&id=cvvkmrfmpr44r4rjj;expires=Fri,31-Dec-9999 23:59:59 GMT;path=/ Set-Cookie: rua-usm2=date=10.09.2012&ptype=1&ts=11;expires=Fri,31-Dec-9999 23:59:59 GMT;path=/ / Set-Cookie: VisitorDone=1;expires=Fri,31-Dec-9999 23:59:59 GMT;path=/

如你所见,有4个曲奇饼。但是当我读到response.Cookies.Count时,我只得到了一个:*ASP.NET_SessionId*。

我使用下一个代码:

代码语言:javascript
运行
复制
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

我尝试手动解析Set-cookie字段:

代码语言:javascript
运行
复制
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;
    }

但有错误:

“cookie的'Domain'='http://xxxx.xxxx‘部分无效。”

据我所知,它与一些非标准人物的设定-曲奇价值有关。但是我用HttpUtility.UrlEncode编码了所有的值!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-10 11:10:06

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

票数 0
EN

Stack Overflow用户

发布于 2013-07-20 18:57:40

您还可能希望将另一个IF语句添加到ConvertCookieArraysToCookieCollection方法中,以获得过期日期。以下内容应该适用于你。

代码语言:javascript
运行
复制
                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;
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12343789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档