我有一个HttpWebRequest来获取会话ID。然后我从响应中获取cookie,将其添加到第二个请求以获取我需要的页面。
使用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();
答案 0 :(得分:0)
我认为它失败了,因为您尝试使用.Credentials属性在基于FormsAuthentication的应用程序中进行身份验证。 Credentials属性适用于Basic或NTLM身份验证,但不适用于Forms身份验证,在这种情况下,您只需要将期望字段POST到Login.aspx页面,以便恢复Auth cookie。