I am developing an application that uses WebClient. I have this class that extends basic WebClient functionality:
public class WebClientEx : WebClient
{
private CookieContainer _cookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _cookieContainer;
(request as HttpWebRequest).AllowAutoRedirect = true;
(request as HttpWebRequest).Timeout = 10000;
}
return request;
}
}
I use WebClientEx to login to site and to request some information. It works well for 4.0 and 4.5, but it doesn't work in earlier versions such as 3.5, 3.0 etc. I added some debug code and in early versions it says that there are 0 cookies in cookie container, while 4.0+ says that there are two cookies, as it should be.
So the reason is probably that early versions of .NET Framework have some problems with storing cookies in the cookie container. How to fix that?