I have a request to a login page, and I am trying to save the response cookies in a CookieContainer . I am then attempting to make another request reusing the previous CookieContainer . I then am trying to save the content of the second response as an .html document. However, it seems as though my second request keeps taking me back to the original URI (login page).
Code:
public void selectFiles()
{
CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://subdomain.domain.com/login.aspx");
request.CookieContainer = myContainer;
request.Credentials = new NetworkCredential("un", "pw");
request.AllowAutoRedirect = false;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "text/html";
request.ContentLength = 0;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://subdomain.domain.com/Default.aspx?GUID=123456");
request2.CookieContainer = myContainer;
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
try
{
Stream responseStream = response2.GetResponseStream();
Encoding enc = Encoding.GetEncoding(response2.CharacterSet);
StreamReader reader = new StreamReader(responseStream, enc);
Stream fileStream = File.OpenWrite(@"c:\test1234.html");
using (StreamWriter sw = new StreamWriter(fileStream, enc))
{
sw.Write(reader.ReadToEnd());
sw.Flush();
sw.Close();
}
}
finally
{
response.Close();
}
Comment