我已经编写了一段代码并且执行得很好,我提供了它实现 POST 的用户名和密码,但我不知道登录是否成功,有什么方法可以检查吗?
var strId = UserName.Text;
var strName = UserPass.Text;
var encoding = new ASCIIEncoding();
var postData = "MainContent_LoginUser_UserName=" + strId + &MainContent_LoginUser_Password=" + strName + "&LoginButton";
byte[] data = encoding.GetBytes(postData);
var myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:6226/WebSite1/Account/Login.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
textBox1.Text = responseReader.ReadToEnd();
最佳答案
您需要 HTTP 响应代码。通常 200 表示成功,401 表示禁止或 302 - 重定向到登录页面。
编辑:实际上,您正在提交到登录页面,因此 302 可能表示成功登录并重定向到默认用户页面,这取决于服务器上启用的身份验证类型以及 login.aspx 背后的逻辑。
关于c# - web请求如何检查用户是否成功登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4500126/