HttpWebRequest.Headers.Add ("Cookie", value) vs HttpWebRequest.CookieContainer

When I get a response from HttpWebRequest with HttpWebRequest.Headers.Add("Cookie",value) vs HttpWebRequest.CookieContainer , and the results are different.

So what is the difference between them and when to use them.

+8
c # cookies cookiecontainer
source share
1 answer

In my experience, I am having problems using the HttpWebRequest.CookieContainer to manage cookies. It may work to some extent, but sometimes cookies are not compatible with the RFC and they are not processed correctly in the CookieContainer . If you manage your files manually by adding them to the Cookie header in the request, you will have better success with some websites that do not comply with RFC requirements. One of the problems with CookieContainer is that if a date such as September 26, 2013 has a semicolon, it will parse it as a separate cookie and break the parsing. Another problem is that cookies set in the HTTP 302 redirect will not display with CookieContainer .

It depends on what is best for your specific requirements, but if problems with the CookieContainer give different results than manually setting the cookie header, I would recommend that you manually set the cookie header. I hope Microsoft updates this in the future so that we can return to using the good .NET classes for cookie management.

Edit:
I came across some code that correctly parses the "Set-Cookie" header. It processes comma separated cookies and retrieves the name, expiration, path, value and domain of each cookie.

This code works better than Microsoft's proprietary cookie parser, and it really is what official parsing a cookie should do. I don’t know why Microsoft has not fixed this yet, as this is a very common problem.

Here is the source code: http://snipplr.com/view/4427/

I post it here if at some point the link goes down:

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

All Articles