Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a desktop application (c#.net) to submit a login.php form and read the response if the login sucess or not


PHP
<?php
?>
<html>
    <body>
        <form action="checklogin.php" method="POST">
            <input type="text" name="name" size="20"><br/>
            <input type="password" name="password" size="20"><br/>
   <input type="submit" name="submit">
</form>
    </body>
</html>


In c# i have created a form having two text fields for name and password

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace loginphp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private String readHtmlPage(string url)
        {

            //setup some variables

            String username = "rk";
            String password = "rk";
            //String firstname = "John";
            //String lastname = "Smith";

            //setup some variables end

            String result = "";
            String strPost = "name=" + username + "&password=" + password ; //+"&firstname=" + firstname + "&lastname=" + lastname;
            StreamWriter myWriter = null;

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";
            objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                myWriter.Close();
            }

            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            using (StreamReader sr =
               new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();

                // Close and clean up the StreamReader
                sr.Close();
            }
            return result;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text= readHtmlPage("http://localhost/spotkeys/login.html");
        }
    }
}


but it only returns the following



<html>
<body>
<form action="checklogin.php" method="POST">
<input type="text" name="name" size="20">

<input type="password" name="password" size="20">

<input type="submit" name="submit">
</form>
</body>
</html>


how can i sumbit a php form using c# and read the response if login sucess or not
Posted
Comments
Rock (Multithreaded) 12-Sep-12 2:08am    
Your code is completely wrong.
Rock (Multithreaded) 12-Sep-12 2:10am    
It does't mean...objRequest.ContentLength = strPost.Length;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900