I'm using a web client that is cookie aware
public class CookieAwareWebClient : WebClient
{
public CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = m_container;
}
return request;
}
public CookieCollection Cookies(Uri domain)
{
return m_container.GetCookies(domain);
}
}
And making a few request to a web site that drops a set of cookies like this
webClient.Cookies(new Uri("http://<domain>.com")).Cast<Cookie>().ToArray()
{System.Net.Cookie[7]}
[0]: {UsrLocale=en_CA}
[1]: {Country=CA}
[2]: {$Version=1; ca_ord="UJMupexgTADsaH1yNi9eyA=="; $Path=/; $Domain=.<domain>.com}
[3]: {isLoggedin=false}
[4]: {cartCount=1}
[5]: {userPrefLanguage=en_CA}
The next request doesn't work as expected, and when I look in fiddler I see that cookie[2] hasn't been added to the headers. This one is pretty important by the looks of things. Does anyone know why it wouldn't be added to the request? All the others get popped in fine, and the domain I'm making a request to is a subdomain of the one listed in the cookie i.e. subDomain..com
var domain = new Uri("http://domain.com");
var webClient = new CookieAwareWebClient();
webClient.OpenRead("http://sub.domain.com");
webClient.RefreshCookies(domain);
webClient.UploadValues("http://sub.domain.com/browse/submit.jsp", new NameValueCollection
{
{"productId","prod610181"},
{"skuId","3431733"},
{"quantity","1"},
{"page","MAIN"}
});
webClient.RefreshCookies(domain);
webClient.OpenRead("http://sub.domain.com/shopping/bag.jsp");
webClient.RefreshCookies(domain);
var order = webClient.Cookies(new Uri("http://domain.com")).Cast<Cookie>().ToArray();