为什么我的HttpWebRequest重定向到登录页面
本文关键字:登录 重定向 我的 HttpWebRequest 为什么 | 更新日期: 2023-09-27 18:08:06
我有一个HttpWebRequest来抓取会话ID。然后从响应中获取cookie,将其添加到第二个请求中以获得所需的页面。
如果使用IIS/7.5,会出现什么情况?我正在使用小提琴手,并获得302状态。我正在获取asp.net 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();
我认为这是失败的,因为你试图使用。credentials属性在基于formsauthentication的应用程序中进行身份验证。凭据属性将适用于基本或NTLM身份验证,但不适用于表单身份验证,在这种情况下,您只需要将预期字段POST到Login。