string url = ConfigurationManager.AppSettings.Get("url");
CookieContainer myCookie
= new CookieContainer
();
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = myCookie;
request.Method = "GET";
request.KeepAlive = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
StreamReader reader
= new StreamReader
(responseStream, Encoding
.UTF8);
string src = reader.ReadToEnd();
//Get ViewState
string VSFlag = "id=\"__VIEWSTATE\" value=\"";
int i = src.IndexOf(VSFlag) + VSFlag.Length;
int j = src.IndexOf("\"", i);
string viewState = src.Substring(i, j - i);
//Get ViewStateGenerator
string VSGenF = "id=\"__VIEWSTATEGENERATOR\" value=\"";
i = src.IndexOf(VSGenF) + VSGenF.Length;
j = src.IndexOf("\"", i);
string viewStateGenerator = src.Substring(i, j - i);
//Event Validation
string eventValidationF = "id=\"__EVENTVALIDATION\" value=\"";
i = src.IndexOf(eventValidationF) + eventValidationF.Length;
j = src.IndexOf("\"", i);
string eventValidation = src.Substring(i, j - i);
string submit = "Sign In";
string user = ConfigurationManager.AppSettings.Get("user");
string password = ConfigurationManager.AppSettings.Get("pass");
viewState = HttpUtility.UrlEncode(viewState);
eventValidation = HttpUtility.UrlEncode(eventValidation);
viewStateGenerator = HttpUtility.UrlEncode(viewStateGenerator);
submit = HttpUtility.UrlEncode(submit);
user = HttpUtility.UrlEncode(user);
password = HttpUtility.UrlEncode(password);
string strPost = "POSTDATA=__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}"
+ "&__VIEWSTATEGENERATOR={1}&__EVENTVALIDATION={2}&ctl00%24PlaceHolderMain%24signInControl%24UserName={3}"
+ "&ctl00%24PlaceHolderMain%24signInControl%24password={4}&ctl00%24PlaceHolderMain%24signInControl%24login={5}";
string postStr = string.Format(strPost, viewState, viewStateGenerator, eventValidation, user, password, submit);
byte[] postData = Encoding.ASCII.GetBytes(postStr);
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.Referer = url;
request.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = myCookie;
Cookie ck
= new Cookie
("SworkCookie",
string.Empty);
ck.Domain = request.RequestUri.Host;
request.CookieContainer.Add(ck);
request.CookieContainer.Add(response.Cookies);
request.CookieContainer = myCookie;
request.ContentLength = postData.Length;
//------------
//submit
Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
//accept
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader
= new StreamReader
(responseStream, Encoding
.UTF8);
string srcString = reader.ReadToEnd();