Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void DoIT()
- {
- try
- {
- HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
- StreamReader responseReader = new StreamReader(
- webRequest.GetResponse().GetResponseStream()
- );
- string responseData = responseReader.ReadToEnd();
- responseReader.Close();
- // extract the viewstate value and build out POST data
- string viewState = ExtractViewState(responseData);
- string EventValidation = ExtractEventValidation(responseData);
- string viewstategen = ExtractViewStateGen(responseData);
- NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
- outgoingQueryString.Add("__VIEWSTATE", viewState);
- outgoingQueryString.Add("__EVENTVALIDATION", EventValidation);
- outgoingQueryString.Add("__VIEWSTATEGENERATOR", viewstategen);
- outgoingQueryString.Add("__EVENTTARGET", string.Empty);
- outgoingQueryString.Add("__EVENTARGUMENT", string.Empty);
- outgoingQueryString.Add("Login1$UserName", "TestUser");
- outgoingQueryString.Add("Login1$Password", "TestPass");
- outgoingQueryString.Add("Login1$SiteDDL", "TestSite");
- outgoingQueryString.Add("ShowHospreferral", "true");
- outgoingQueryString.Add("Login1$LoginButton", "Login");
- var data = Encoding.ASCII.GetBytes(outgoingQueryString.ToString());
- string postData =
- String.Format(
- "__VIEWSTATE={0}& __EVENTVALIDATION={1}&__EVENTTARGET={2}&__EVENTARGUMENT={3}&__VIEWSTATEGENERATOR={4}&Login1$UserName={5}&Login1$Password={6}&Login1$SiteDDL={6}&ShowHospreferral={7}&Login1$LoginButton={8}",
- viewState ,EventValidation, HttpUtility.UrlEncodeUnicode(string.Empty), HttpUtility.UrlEncodeUnicode(string.Empty), viewstategen , "Dougie", "Cardiology1", "T101H", HttpUtility.UrlEncodeUnicode("true"), "Login"
- );
- // have a cookie container ready to receive the forms auth cookie
- CookieContainer cookies = new CookieContainer();
- // now post to the login form
- webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
- webRequest.Method = "POST";
- webRequest.ContentType = "application/x-www-form-urlencoded";
- webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
- webRequest.Headers.Set("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8");
- webRequest.Headers.Set("Accept-Encoding", "gzip, deflate, br");
- webRequest.Referer = LOGIN_URL;
- webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
- webRequest.CookieContainer = cookies;
- // write the form values into the request message
- StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
- requestWriter.Write(postData);
- requestWriter.Close();
- // we don't need the contents of the response, just the cookie it issues
- webRequest.GetResponse().Close();
- // now we can send out cookie along with a request for the protected page
- webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
- webRequest.CookieContainer = cookies;
- responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
- // and read the response
- responseData = responseReader.ReadToEnd();
- responseReader.Close();
- textBox1.Text = responseData;
- }
- catch (WebException webex)
- {
- WebResponse errResp = webex.Response;
- using (Stream respStream = errResp.GetResponseStream())
- {
- StreamReader reader = new StreamReader(respStream);
- textBox1.Text = reader.ReadToEnd();
- }
- }
- catch (Exception ex)
- { textBox1.Text = ex.Message; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement