Click here to Skip to main content
Sign Up to vote bad
good
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 - 19:58

Comments
Arun (Multithreaded) - 12 Sep '12 - 2:08
Your code is completely wrong.
Arun (Multithreaded) - 12 Sep '12 - 2:10
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 345
1 Sergey Alexandrovich Kryukov 338
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,549
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


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