POST数据从C#WinForm的一个PHP页面 [英] POST data to a PHP page from C# WinForm

查看:866
本文介绍了POST数据从C#WinForm的一个PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms NET3.5SP1应用程序,并希望POST数据到PHP页面。

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

我也将要经过它作为JSON,但。想要获得连胜POST第一个工作日

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

下面是代码:

    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 ();

和PHP5.2的代码是:

And the PHP5.2 code is:

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

这将返回回:

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.

推荐答案

我相信您需要正确编码和发送实际的文章内容。它看起来像你只是序列化为JSON,其中PHP不知道该怎么做(即,它不会将其设置为 $ _ POST 值)

i believe you need to properly encode and send the actual post content. it looks like you're just serializing into JSON, which PHP doesn't know what to do with (ie, it won't set it as $_POST values)

string postData = "firstName=" + HttpUtility.UrlEncode(p.firstName) +
                  "&lastName=" + HttpUtility.UrlEncode(p.lastName) +                    
                  "&email=" + HttpUtility.UrlEncode(p.email) +
                  "&deviceUUID=" + HttpUtility.UrlEncode(p.deviceUUID);
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
// etc...

这应该得到你的 $在PHP中集_ POST 变量。以后当你切换到JSON,你可以这样做:

this should get your $_POST variable in PHP set. later when you switch to JSON you could do something like:

string postData = "json=" + HttpUtility.UrlEncode(serializer.Serialize(p) );

和抓斗从PHP:

$json_array = json_decode($_POST['json']);

这篇关于POST数据从C#WinForm的一个PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆