2012-09-09 28 views
3

我想收到并存入CookieContainer多个Cookie。如何从WebResponse获取多个Set-Cookie?

Set-Cookie: 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 *。

我用下面的代码:

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

,但得到的错误:

"The 'Domain'='http://xxxx.xxxx' part of the cookie is invalid."

据我了解一些相关的不会将标准字符转换为Set-Cookie值。但我编码的所有值由HttpUtility.UrlEncode

回答

0

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

0

您还可能需要将另一个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; 
       }