Donate

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
ccContainer.SetCookies(webResponse.ResponseUri, 
        HttpUtility.UrlEncode(webResponse.Headers["Set-Cookie"], Encoding.GetEncoding("iso-8859-1"))); 

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input