Httpwebrequest Cookiecontainer

How to handle cookies with paths other than "/". The 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 collection of cookies contains only cookies with an outline "/". So cookiecontainer only has cookie1 and cookie4.

Why aren't the rest going to? How to access cookies with paths other than "/"? Can I collect them all in one container?

thanks

+4
source share
2 answers

Given how often this problem occurs on the Internet, I suspect that the problem is that the .NET library code does not support multiple Set-Cookie headers (either all the time, or only under certain circumstances). Despite this, it's pretty easy to get around. Just extract the cookies directly from the Set-Cookie headers. Here's some code (originally copied from the code attached to this stream ) that 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; } 
+8
source

In fact, CookieContainer received all cookies, but you just cannot see them. When you get it from the GelCookies () method, it will provide you with suitable cookies based on the current path and domain.

CookieContainer will process the domain, path and expiration time, but only it has an error while processing subdomains.

Check this out for details and corrections: http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html

+4
source

Source: https://habr.com/ru/post/1286014/


All Articles