0

EDITED

I managed to make a simple app allows me to login to the site but the problem is I need to make the app redirect to another page when I enter the correct username and password.

Notice that upon entering the wrong username or password a message pops up and contains the whole source code of the site.

How can I select a certain phrase like " Error " from the source code to tell the app that this there is an incorrect username or password or make that phrase the only thing that pops up?

    private void SubmitData()
    {
        try
        {
            string username = textBox1.Text;
            string password = textBox2.Text;
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "username" + username+ "&password" + password;
            byte[] data = encoding.GetBytes(postData);

            WebRequest request = WebRequest.Create("url");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();

            StreamReader sr = new StreamReader(stream);
            MessageBox.Show(sr.ReadToEnd());

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message);
        }
AlG
  • 14,697
  • 4
  • 41
  • 54
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16
  • to use a text box where you would enter your user name and password is quite simple.. then store the validated information as a boolean for example in a Session variable.. then when logging out you would kill that session meaning set the session variable = null .. if you are using a Global.asax file you would initialize the session variable as in the OnSession_Start there are plenty of examples on how to do this on the web. do a google search on `JavaScript window.close()` function – MethodMan Mar 11 '16 at 22:36
  • @MethodMan thanks for answering me, but i'm not expert yet and this is my first project . I understood what you have said but i don't really know how to apply – Ahmed Soliman Mar 11 '16 at 22:46
  • then do a google search on how to create a proper login page for a website. this is literally not that difficult.. – MethodMan Mar 11 '16 at 22:55
  • @MethodMan i did several researchs that code i posted i understand it all, but on making it manual using 2 textboxs i keep getting errors i don't know how to fix – Ahmed Soliman Mar 11 '16 at 23:02
  • well use the debugger.. read the error code if you are not sure what it's saying ..then google the error code.. also `I keep getting errors` does nobody any good since you have not posted any code in regards to the textboxes nor the error messages themselves.. please read the link on the top right of the page about how to ask a question – MethodMan Mar 11 '16 at 23:08
  • If you want to scrape the resulting HTML for values, consider trying the HTML Agility Pack. Check out this post on how to use it! http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack – DaFi4 Mar 14 '16 at 12:51
  • here are some other alternatives to consider, as well: http://stackoverflow.com/questions/1065031/is-the-html-agility-pack-still-the-best-net-html-parser – DaFi4 Mar 14 '16 at 12:52

1 Answers1

1

You can scrap the source code by using a regex or any method:

try
{
     string source = sr.ReadToEnd(); // read the source code
     MatchCollection m0 = Regex.Matches(source, "(err")(?<td_inner>.*?)(\">)", RegexOptions.Singleline); // scrap the data you want
     foreach (Match m in m0) // loop through that data 
     { 
         string data= m.Groups["td_inner"].Value; // getting the data value
         txt_box_message.Text = data; // showing the data into a textBox
     }
} catch(Exception ex) // catch any error
{
     MessageBox.Show(ex.Message); // show that error in message box
}
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16