Get All Cookies From HTTP Header


/ Published in: C#
Save to your folder(s)

drunkenprogrammer


Copy this code and paste it in your HTML
  1. public static CookieCollection GetAllCookiesFromHeader(string strHeader, string strHost)
  2. {
  3. ArrayList al = new ArrayList();
  4. CookieCollection cc = new CookieCollection();
  5. if (strHeader != string.Empty)
  6. {
  7. al = ConvertCookieHeaderToArrayList(strHeader);
  8. cc = ConvertCookieArraysToCookieCollection(al, strHost);
  9. }
  10. return cc;
  11. }
  12.  
  13.  
  14. private static ArrayList ConvertCookieHeaderToArrayList(string strCookHeader)
  15. {
  16. strCookHeader = strCookHeader.Replace("\r", "");
  17. strCookHeader = strCookHeader.Replace("\n", "");
  18. string[] strCookTemp = strCookHeader.Split(',');
  19. ArrayList al = new ArrayList();
  20. int i = 0;
  21. int n = strCookTemp.Length;
  22. while (i < n)
  23. {
  24. if (strCookTemp[i].IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0)
  25. {
  26. al.Add(strCookTemp[i] + "," + strCookTemp[i + 1]);
  27. i = i + 1;
  28. }
  29. else
  30. {
  31. al.Add(strCookTemp[i]);
  32. }
  33. i = i + 1;
  34. }
  35. return al;
  36. }
  37.  
  38.  
  39. private static CookieCollection ConvertCookieArraysToCookieCollection(ArrayList al, string strHost)
  40. {
  41. CookieCollection cc = new CookieCollection();
  42.  
  43. int alcount = al.Count;
  44. string strEachCook;
  45. string[] strEachCookParts;
  46. for (int i = 0; i < alcount; i++)
  47. {
  48. strEachCook = al[i].ToString();
  49. strEachCookParts = strEachCook.Split(';');
  50. int intEachCookPartsCount = strEachCookParts.Length;
  51. string strCNameAndCValue = string.Empty;
  52. string strPNameAndPValue = string.Empty;
  53. string strDNameAndDValue = string.Empty;
  54. string[] NameValuePairTemp;
  55. Cookie cookTemp = new Cookie();
  56.  
  57. for (int j = 0; j < intEachCookPartsCount; j++)
  58. {
  59. if (j == 0)
  60. {
  61. strCNameAndCValue = strEachCookParts[j];
  62. if (strCNameAndCValue != string.Empty)
  63. {
  64. int firstEqual = strCNameAndCValue.IndexOf("=");
  65. string firstName = strCNameAndCValue.Substring(0, firstEqual);
  66. string allValue = strCNameAndCValue.Substring(firstEqual + 1, strCNameAndCValue.Length - (firstEqual + 1));
  67. cookTemp.Name = firstName;
  68. cookTemp.Value = allValue;
  69. }
  70. continue;
  71. }
  72. if (strEachCookParts[j].IndexOf("path", StringComparison.OrdinalIgnoreCase) >= 0)
  73. {
  74. strPNameAndPValue = strEachCookParts[j];
  75. if (strPNameAndPValue != string.Empty)
  76. {
  77. NameValuePairTemp = strPNameAndPValue.Split('=');
  78. if (NameValuePairTemp[1] != string.Empty)
  79. {
  80. cookTemp.Path = NameValuePairTemp[1];
  81. }
  82. else
  83. {
  84. cookTemp.Path = "/";
  85. }
  86. }
  87. continue;
  88. }
  89.  
  90. if (strEachCookParts[j].IndexOf("domain", StringComparison.OrdinalIgnoreCase) >= 0)
  91. {
  92. strPNameAndPValue = strEachCookParts[j];
  93. if (strPNameAndPValue != string.Empty)
  94. {
  95. NameValuePairTemp = strPNameAndPValue.Split('=');
  96.  
  97. if (NameValuePairTemp[1] != string.Empty)
  98. {
  99. cookTemp.Domain = NameValuePairTemp[1];
  100. }
  101. else
  102. {
  103. cookTemp.Domain = strHost;
  104. }
  105. }
  106. continue;
  107. }
  108. }
  109.  
  110. if (cookTemp.Path == string.Empty)
  111. {
  112. cookTemp.Path = "/";
  113. }
  114. if (cookTemp.Domain == string.Empty)
  115. {
  116. cookTemp.Domain = strHost;
  117. }
  118. cc.Add(cookTemp);
  119. }
  120. return cc;
  121. }

URL: ideabubbling.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.