C# Application CookieContainer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DonBytes
    New Member
    • Aug 2008
    • 25

    C# Application CookieContainer

    Wondering if anyone had problems with the CookieContainer s, I was making a program that logs onto sites, check for news and then reports back.

    All cookies domain in response.cookie s is marked as site.com except for one that is marked as .site.com for some reason this doesn't make it back to http://site.com when I try to check the messages page after doing the login.

    If I override the WebResponse in WebClient and add a copy of that cookie without the initial dot all is well and I get my result. However this is not nice as I get extra cookies so I was wondering if anyone knew why?
    Last edited by DonBytes; Sep 8 '08, 06:14 PM. Reason: Stupid title as I missed the important word
  • DonBytes
    New Member
    • Aug 2008
    • 25

    #2
    To explain further:

    Code:
    public class WebClientEx : WebClient
    	{
    		private CookieContainer cookies = new CookieContainer();
    
    		protected override WebRequest GetWebRequest(Uri address)
    		{
    			WebRequest retVal = base.GetWebRequest(address);
    
    			if (retVal.GetType() == typeof(HttpWebRequest))
    			{
    				((HttpWebRequest)retVal).CookieContainer = cookies;
    			}
    
    			return retVal;
    		}
    
    		protected override WebResponse GetWebResponse(WebRequest request)
    		{
    			WebResponse retVal = base.GetWebResponse(request);
    
    			if (retVal.GetType() == typeof(HttpWebResponse))
    			{
    				foreach (Cookie c in ((HttpWebResponse)retVal).Cookies)
    				{
    					if (c.Domain.StartsWith("."))
    					{
    						c.Domain = c.Domain.Substring(1);
    						cookies.Add(c);
    					}
    				}
    			}
    
    			return retVal;
    		}
    	}
    Not sure why the stuff in GetWebResponse has to be done but without it it doesn't work to login on some sites. So I am curious if someone knows what is different in WebClient from a Browser regarding cookie handling.

    Comment

    Working...