HttpWebRequest的的CookieContainer [英] httpwebrequest Cookiecontainer
问题描述
我如何处理cookie比/其他路径。 一个HttpWebRequest对象返回这些标头:
HTTP / 1.1 302临时移动
传输编码:分块
日期:周三2009 GMT 13时22分53秒6月10日
内容类型:text / html的;字符集= UTF-8
到期日:周三2009 GMT十三时22分53秒6月10日
的Cache-Control:no-cache的,必重新验证,最大年龄= 0
服务器:nginx / 0.7.41
的X已启动方式:PHP / 5.2.9
最后一次修改:周三2009年6月10日13时22分52秒格林尼治标准时间
杂注:无缓存
设置Cookie:COOKIE1 = C1;路径= /;域= site.com
设置Cookie:COOKIE2 = C2; PATH = /内容;域= site.com;仅Http
设置Cookie:cookie3 = C3; PATH = /管理;域= site.com;仅Http
设置Cookie:cookie4 = 4;路径= /;域= site.com;仅Http
地点:http://site.com/admin/
途经:1.1 MVO-的NetCache-02(的NetCache的NetApp / 6.0.7)
迭代通过Cookie集合只containsthe饼干的/的路径。 所以的CookieContainer只有COOKIE1和cookie4在里面。
为什么其他地方没有被收集的? 如何访问饼干比/其他路径?我可以收集他们都 在一个容器?</ P>
感谢
由于频率这个问题出现在网上,我怀疑的问题是,.NET库code不支持多的Set-Cookie头(要么所有的时间或仅在某些情况下)。无论如何,这是pretty的容易解决。只需直接在Set-Cookie头中提取的cookies。下面是一些code(最初是从code复制连接到<一个href="http://www.velocityreviews.com/forums/t120691-cookiecontainer-getcookies-fails-on-multiple-setcookies.html">this螺纹),它展示了如何直接在Set-Cookie头中提取的cookies。
公共静态CookieCollection GetAllCookiesFromHeader(字符串strHeader,串strHost)
{
ArrayList的人=新的ArrayList();
CookieCollection CC =新CookieCollection();
如果(strHeader!=的String.Empty)
{
人= ConvertCookieHeaderToArrayList(strHeader);
CC = ConvertCookieArraysToCookieCollection(人,strHost);
}
返回CC;
}
私有静态的ArrayList ConvertCookieHeaderToArrayList(字符串strCookHeader)
{
strCookHeader = strCookHeader.Replace(\ r,);
strCookHeader = strCookHeader.Replace(\ N,);
串[] strCookTemp = strCookHeader.Split(,);
ArrayList的人=新的ArrayList();
INT I = 0;
INT N = strCookTemp.Length;
而(ⅰn种)
{
如果(strCookTemp [I] .IndexOf(期满=,StringComparison.OrdinalIgnoreCase)大于0)
{
al.Add(strCookTemp [I] +,+ strCookTemp [I + 1]);
I = I + 1;
}
其他
{
al.Add(strCookTemp [I]);
}
I = I + 1;
}
返回人;
}
私有静态CookieCollection ConvertCookieArraysToCookieCollection(ArrayList的人,串strHost)
{
CookieCollection CC =新CookieCollection();
INT alcount = al.Count;
串strEachCook;
字符串[] strEachCookParts;
的for(int i = 0; I&LT; alcount;我++)
{
strEachCook =人[我]的ToString();
strEachCookParts = strEachCook.Split(';');
INT intEachCookPartsCount = strEachCookParts.Length;
字符串strCNameAndCValue =的String.Empty;
字符串strPNameAndPValue =的String.Empty;
字符串strDNameAndDValue =的String.Empty;
字符串[] NameValuePairTemp;
曲奇cookTemp =新的Cookie();
对于(INT J = 0; J&LT; intEachCookPartsCount; J ++)
{
如果(j == 0)
{
strCNameAndCValue = strEachCookParts [J]。
如果(strCNameAndCValue!=的String.Empty)
{
INT firstEqual = strCNameAndCValue.IndexOf(=);
串的firstName = strCNameAndCValue.Substring(0,firstEqual);
串allValue = strCNameAndCValue.Substring(firstEqual + 1,strCNameAndCValue.Length - (firstEqual + 1));
cookTemp.Name =名字;
cookTemp.Value = allValue;
}
继续;
}
如果(strEachCookParts [J] .IndexOf(路径,StringComparison.OrdinalIgnoreCase)&GT; = 0)
{
strPNameAndPValue = strEachCookParts [J]。
如果(strPNameAndPValue!=的String.Empty)
{
NameValuePairTemp = strPNameAndPValue.Split('=');
如果(NameValuePairTemp [1]!=的String.Empty)
{
cookTemp.Path = NameValuePairTemp [1];
}
其他
{
cookTemp.Path =/;
}
}
继续;
}
如果(strEachCookParts [J] .IndexOf(域,StringComparison.OrdinalIgnoreCase)&GT; = 0)
{
strPNameAndPValue = strEachCookParts [J]。
如果(strPNameAndPValue!=的String.Empty)
{
NameValuePairTemp = strPNameAndPValue.Split('=');
如果(NameValuePairTemp [1]!=的String.Empty)
{
cookTemp.Domain = NameValuePairTemp [1];
}
其他
{
cookTemp.Domain = strHost;
}
}
继续;
}
}
如果(cookTemp.Path ==的String.Empty)
{
cookTemp.Path =/;
}
如果(cookTemp.Domain ==的String.Empty)
{
cookTemp.Domain = strHost;
}
cc.Add(cookTemp);
}
返回CC;
}
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)
Iterating through a cookie collection only containsthe cookies with a path of "/". So the cookiecontainer only has cookie1 and cookie4 in it.
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?
Thanks
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("\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 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的的CookieContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!