Using windowsform: POST data to a PHP page from C# WinForm on newest questions tagged winforms – Stack Overflow

I have a winForms NET3.5SP1 app, and want to POST data to a PHP page.

I’m also going to be passing it as JSON, but wanted to get straight POST working first.

Here is the code:

    Person p = new Person();
    p.firstName = "Bill";
    p.lastName = "Gates";
    p.email = "asdf@hotmail.com";
    p.deviceUUID = "abcdefghijklmnopqrstuvwxyz";

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string s;
    s = serializer.Serialize(p);
    textBox3.Text = s;
    // s = "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"asdf@hotmail.com\",\"deviceUUID\":\"abcdefghijklmnopqrstuvwxyz\"}"
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.davemateer.com/ig/genius/newuser.php");
    //WebRequest request = WebRequest.Create("http://www.davemateer.com/ig/genius/newuser.php");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    //byte[] byteArray = Encoding.UTF8.GetBytes(s);
    byte[] byteArray = Encoding.ASCII.GetBytes(s);
    request.ContentLength = byteArray.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close ();

    WebResponse response = request.GetResponse();
    textBox4.Text = (((HttpWebResponse)response).StatusDescription);
    dataStream = response.GetResponseStream ();

    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd ();
    textBox4.Text += responseFromServer;

    reader.Close ();
    dataStream.Close ();
    response.Close ();

And the PHP5.2 code is:

<?php
echo "hello world";
var_dump($_POST);
?>

this returns back:

array(0) {}

Any ideas? I want it return the values that I just passed it to prove I can access the data from the server side.

See Answers


source: http://stackoverflow.com/questions/304337/post-data-to-a-php-page-from-c-sharp-winform
Using windowsform: using-windowsform



online applications demo