问题描述
我想接收并存储到 CookieContainer
多个Cookie。
服务器在一个请求期间发送这个代码给我(我看到这行到Fiddler):
I want to receive and store into CookieContainer
multiple cookies.Server send this code during one request to me (I see this lines into Fiddler):
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-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: 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 = /
Set-Cookie: VisitorDone=1; expires=Fri, 31-Dec-9999 23:59:59 GMT; path=/
,因为您看到有4个Cookie。但是当我阅读 response.Cookies.Count
我只得到一个:* ASP.NET_SessionId *。
as you see there are 4 cookies. But when I read response.Cookies.Count
I get only one: *ASP.NET_SessionId*.
下一个代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
我尝试手动解析Set-cookie字段:
I tried parse Set-cookie field manually:
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;
}
但出现错误:
根据我的理解,它涉及到一些不是standart字符到Set-Cookie值。但我编码所有的值 HttpUtility.UrlEncode
!
As I understand it related to some not standart characters into Set-Cookie value. But I encoded all values by HttpUtility.UrlEncode
!
推荐答案
与 CookieCollection cookiesList = GetAllCookiesFromHeader(cookiesText,host);
行。 host
变量不得包含http://字符串。
Problem was with CookieCollection cookiesList = GetAllCookiesFromHeader(cookiesText, host);
line. host
variable must not contains "http://" string.
这篇关于如何自WebResponse得到多个设置Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!