我有一个HttpWebRequest来获取会话ID。然后,我从响应中获取cookie,将其添加到第二个请求以获取我需要的页面。为什么我的HttpWebRequest重定向到登录页面?
使用IIS/7.5,这种失败有哪些可能的情况? 我正在使用Fiddler,并获得302状态。我正在获取ASPNET SessionID。
CookieContainer myCookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://www.secure.com/login.aspx");
req.Method = "POST";
req.Credentials = fileretrieve.Credentials;//Network credentials.
req.CookieContainer = myCookies;
req.AllowAutoRedirect = true;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(req.Credentials.ToString());
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
HttpWebRequest xmlreq = (HttpWebRequest)HttpWebRequest.Create("https://www.secure.com/file");
xmlreq.Method = "GET";
xmlreq.KeepAlive = true;
xmlreq.AllowAutoRedirect = true;
xmlreq.CookieContainer = req.CookieContainer;
HttpWebResponse xmlresp = (HttpWebResponse)xmlreq.GetResponse();
string webpage;
System.IO.StreamReader sr = new System.IO.StreamReader(xmlresp.GetResponseStream());
webpage = sr.ReadToEnd();
没有显示代码,很难提供帮助。 – Icarus
几个可能的场景 - 有一个HTTP模块运行一些代码,强制在代码之外重定向。检查您的Global.asax代码处理请求是否与您期望的不同。 – EtherDragon