Want a cookie for you or foreach of you?

Ran into a weird cookie issue
HttpWebRequest cookies are disabled for security by default.
You need to enable it. Now if you want to add cookies from your current request, since they are seperate types, you have add it manually.
Example

my httpCookies – HttpContext.Current.Request.Cookies;
var request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer(); //now your cookie is enabled.

//Following foreach will error out with
//unable to cast object of type ‘System.string’ to type ‘System.Web.HttpCookie’
if (HttpContext.Current.Request.Cookies.Count > 0)
{
foreach (HttpCookie requestCookie in HttpContext.Current.Request.Cookies) //this will error out
{
//something I want to do
}
}

You need to use for loop, and it will not error out. Not sure why this happens, but this is a workaround
for(var i=0; i

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Undefined index: cookies in C:\hostingspaces\hoquecon\wordpress\wwwroot\wp-content\plugins\live-composer-page-builder\modules\tp-comments-form\module.php on line 1638