HttpWebRequest request;
HttpWebResponse response;
CookieCollection cookies = new CookieCollection();
request = (HttpWebRequest)WebRequest.Create("http://47.97.109.186/cas/login");
request.Method = "GET";
request.Referer = "";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
request.ContentType = "text/html;charset=UTF-8";
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
cookies = response.Cookies;
html = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
html = reader.ReadToEnd();
}
//var doc = new HtmlDocument();
//doc.LoadHtml(html);
//HtmlNode ltNode = doc.DocumentNode.SelectSingleNode("//input[@name='lt']");
//string lt = ltNode.Attributes["value"].Value;
Match m = Regex.Match(html, @"<script>var lt = ""([^""]+)""", RegexOptions.IgnoreCase);
string lt = m.Success ? m.Groups[1].Value : "";
request = (HttpWebRequest)WebRequest.Create("http://47.97.109.186/cas/login");
request.Method = "POST";
request.Referer = "http://47.97.109.186/cas/login";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
string postData = "username=3e4r5t6y&password=123456&_eventId=submit<=" + lt;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postData);
request.ContentLength = postdatabyte.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postdatabyte, 0, postdatabyte.Length);
}
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
cookies = response.Cookies;
html = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
html = reader.ReadToEnd();
}