programmatically login to a wep page

csharp , aspx Bern, Switzerland
  • 10 years ago
    Hello! I have a url to a web page which requires a the login credentials which i do have.... so i want to programmatically login to that web page by passing username and password.. The code below doesn't seem to work because it doesn't pass the login page.. so anyone PLEASE HELP.. WebClient webClient = new WebClient(); // first, request the login form to get the viewstate value HttpWebRequest webRequest = WebRequest.Create("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 postData = String.Format( "__VIEWSTATE={0}&txtUserName={1}&txtPassword={2}&btnLogin=Login", viewState, "name", "Password" ); // webClient.UploadData("url", "POST", Encoding.ASCII.GetBytes(postData)); // have a cookie container ready to receive the forms auth cookie CookieContainer cookies = new CookieContainer(); // now post to the login form webRequest = WebRequest.Create("url") as HttpWebRequest; webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; 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 //WebResponse wb = webRequest.GetResponse(); //string gg = wb.ToString(); //wb.Close(); // now we can send out cookie along with a request for the protected page webRequest = WebRequest.Create("url") as HttpWebRequest; webRequest.CookieContainer = cookies; responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()); // and read the response responseData = responseReader.ReadToEnd(); responseReader.Close(); } private string ExtractViewState(string s) { string viewStateNameDelimiter = "__VIEWSTATE"; string valueDelimiter = "value=\""; int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter); int viewStateValuePosition = s.IndexOf( valueDelimiter, viewStateNamePosition ); int viewStateStartPosition = viewStateValuePosition + valueDelimiter.Length; int viewStateEndPosition = s.IndexOf("\"", viewStateStartPosition); return HttpUtility.UrlEncodeUnicode( s.Substring( viewStateStartPosition, viewStateEndPosition - viewStateStartPosition ) ); }

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup