我正在尝试从登录中提取数据,以便我能够验证登录是否成功。
如何使用HttpWebRequest提取页面内容,以便我可以使用它来验证登录是否正常。
由于每次登录返回状态代码200,如果登录正确且不正确,则会更难验证成功登录。
private int[] LoginCheck(string TargetWebApp)
{
int[] result = new int[2];
var watch = System.Diagnostics.Stopwatch.StartNew();
try
{
string formUrl = "https://someweb/user/login/default";
string formParams = string.Format("email={0}&password={1}&submit=Login", "someuser@somedomain.com", "somepassword");
string cookieHeader;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
var elapsedMs = watch.ElapsedMilliseconds;
watch.Stop();
var isInvalidAccess = resp.StatusCode == HttpStatusCode.Unauthorized;
Console.WriteLine("Login to something succeed in {0} Milliseconds", elapsedMs);
cookieHeader = resp.Headers["Set-cookie"];
result[0] = 1;
result[1] = (int)elapsedMs;
}
catch (Exception e)
{
//Any exception will return false.
Console.WriteLine(e.Message);
result[0] = 0;
result[1] = 0;
}
return result;
}