I'm trying to get cookies form a loginform, saved it on my cookie container cookieJar, and using in the next request.
The cookies are saved correctly(at least,the count shows an appropiate quantity,but when doing the webrequest3, I not getting the content , getting the page as not logged in.
P.D: I read the related posts, but the major are not completly implemented(obviously),and the others are doing exactly as I do, so I'm in a loss.
CookieContainer cookieJar = new CookieContainer();
//The First Req
HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create("url1");
webRequest1.Method = "GET";
webRequest1.ContentType = "text/html";
webRequest1.KeepAlive = true;
webRequest1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36";
webRequest1.Host = "url1";
webRequest1.CookieContainer = cookieJar;
webRequest1.ContentType = "text/html";
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest1.GetResponse();
Console.WriteLine(cookieJar.Count.ToString());
StreamReader reader = new StreamReader(webResponse.GetResponseStream());
// Read the content fully up to the end.
string responsereq = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
webResponse.Close();
Console.ReadKey();
//Second Request
HttpWebRequest webRequest3 = (HttpWebRequest)WebRequest.Create("url2");
webRequest3.Method = "GET";
webRequest3.KeepAlive = true;
webRequest3.UserAgent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36";
webRequest3.Host = "url2";
webRequest3.CookieContainer = cookieJar;
webRequest3.ContentType = "text/html";
Console.WriteLine(cookieJar.Count.ToString() +"CookieJar");
Console.ReadKey();
webResponse = (HttpWebResponse)webRequest3.GetResponse();
StreamReader reader3 = new StreamReader(webResponse.GetResponseStream());
// Read the content fully up to the end.
string responseFromServer = reader3.ReadToEnd();
Console.WriteLine(responseFromServer);
// Clean up the streams.
webResponse.Close();
Console.ReadKey();
EDIT:
I get with fiddler that when loggin from the explorer,enter to a page automatically after the webrequest1, don't save any cookie,but seems to use some server-side check, that if you don't enter to that page before the webrequest3, the webrequest2 didn't recognize your login.
So, creating another webrequest before webrequest3, do the trick.