我正尝试登录这个网站:https://lms.nust.edu.pk/portal/login/index.php
以下是我的代码:
const string uri = "https://lms.nust.edu.pk/portal/login/index.php";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri);
wr.KeepAlive = true;
wr.Method = "POST";
wr.AllowAutoRedirect = false;
wr.Proxy = p;
wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
wr.ContentType = "application/x-www-form-urlencoded";
wr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36";
string pdata = "username=USERNAME&password=PASSWORD";
byte[] data = UTF8Encoding.UTF8.GetBytes(pdata);
wr.ContentLength = data.Length;
CookieContainer cookie = new CookieContainer();
wr.CookieContainer = cookie;
using (Stream poststream = wr.GetRequestStream())
{
poststream.Write(data, 0, data.Length);
}
HttpWebResponse wp = (HttpWebResponse)wr.GetResponse();
wr.CookieContainer.Add(wp.Cookies);
网站无法通过登录页面。以下是已知的情况。在wp.Cookies
中没有任何cookie。我调试了代码,结果发现尽管我已经指定了uri并设置了AllowAutoRedirect=false
,但它仍然不能前往该uri和另一个URL。我不知道为什么或如何解决。