Posts

Showing posts with the label CookieContainer

Donate

Remove Cookies From CookieContainer Class

The CookieContainer class does not contain a method that clears or removes the cookies associated with it. To remove them, you have to set each cookie's Expired property to True as shown below. C# 1 2 3 4 var cookies = cookieContainer.GetCookies( new Uri( "http://your_url_example_here" )); foreach (Cookie co in cookies) { co.Expired = true ; } VB.NET 1 2 3 4 Dim cookies = cookieContainer.GetCookies( New Uri( "http://your_url_example_here" )) For Each co As Cookie In cookies co.Expired = True Next

CookieContainer - Part Of Cookie Is Invalid In C# Cookies And WebResponse

Given the code below when scraping a website, you might encounter an error which is stated in the title of the post that is Part Of Cookie Is Invalid . 1 2 3 4 5 6 7 8 9 10 11 WebRequest request = WebRequest.Create( "http://your_url_here" ); WebResponse webResponse = request.GetResponse(); if (webResponse.Headers[ "Set-Cookie" ] != null ) { Cookie m_ccCookies = new Cookie(); CookieContainer ccContainer = new CookieContainer(); ccContainer = new CookieContainer(); ccContainer.SetCookies(webResponse.ResponseUri, webResponse.Headers[ "Set-Cookie" ]); ccContainer.Add(ccContainer.GetCookies(webResponse.ResponseUri)); } The issue could be that the url domain cookie value which is passed to SetCookies method have unrecognized characters that needs to be encoded. So, the workaround is to encode the cookie value first and then assign the appropriate encoding before saving it to the cookie container object. 1 2 ccC

Donate