I am totally confused. Can someone please illuminate what is going on
under the hood in this piece of code from John Lewis.
My main confusion is how the cookieContainer can be passed to the
subsequent request if it is not assigned anywhere?
So far this is what I understand:
-Creates New cookieContainer
CookieContainer cookieContainer = new CookieContainer();
-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;
// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code
I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod