Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PHP
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
?>
<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
 
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 11-Sep-12 20:58pm
Comments
Arun (Multithreaded) at 12-Sep-12 2:08am
   
Your code is completely wrong.
Arun (Multithreaded) at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100