将数据从 C# WinForm POST 到 PHP 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/304337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:25:42  来源:igfitidea点击:

POST data to a PHP page from C# WinForm

c#phpwinforms

提问by Dave Mateer

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

我有一个 winForms NET3.5SP1 应用程序,想将数据发布到 PHP 页面。

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

我也将把它作为 JSON 传递,但希望首先直接 POST 工作。

Here is the code:

这是代码:

    Person p = new Person();
    p.firstName = "Bill";
    p.lastName = "Gates";
    p.email = "[email protected]";
    p.deviceUUID = "abcdefghijklmnopqrstuvwxyz";

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string s;
    s = serializer.Serialize(p);
    textBox3.Text = s;
    // s = "{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"email\":\"[email protected]\",\"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:

而 PHP5.2 代码是:

<?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.

有任何想法吗?我希望它返回我刚刚传递给它的值,以证明我可以从服务器端访问数据。

采纳答案by Owen

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 $_POSTvalues)

我相信您需要正确编码并发送实际的帖子内容。看起来您只是序列化为 JSON,PHP 不知道如何处理(即,它不会将其设置为$_POST值)

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...

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

这应该$_POST在 PHP 中设置您的变量。稍后当您切换到 JSON 时,您可以执行以下操作:

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

and grab from PHP:

并从 PHP 中抓取:

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