C#没有将POST数据发送到PHP脚本

时间:2016-06-23 11:49:59

标签: c# php

所以我有一个C#脚本,它通过我的PHP脚本将数据存储到数据库中。我的C#脚本使用WebClient来实现此效果。但遗憾的是我的C#脚本没有向我的PHP脚本发送POST数据,我不知道为什么。这就是我到目前为止所做的:

C#

json = "{\"bedrijfsNaam\":\"" + bedrijfsNaam + "\"," +
       "\"ContPers\":\"" + ContPers + "\"," +
       "\"TelNum\":\"" + TelNum + "\"," +
       "\"email\":\"" + email + "\"," +
       "\"Land\":\"" + Land + "\"," +
       "\"Plaats\":\"" + Plaats + "\"," +
       "\"PostCode\":\"" + PostCode + "\"}";

var b64bytes = System.Text.Encoding.UTF8.GetBytes(json);
b64encode = System.Convert.ToBase64String(b64bytes);

using (WebClient client = new WebClient())
{
    byte[] sendB64 = client.UploadData("http://" + ConfigurationManager.AppSettings["scripturi"].ToString() + "SalesKicker.php", "POST",
    System.Text.Encoding.ASCII.GetBytes("b64string=" + b64encode + "&filename=" + dt.bedrijfsNaam));
    MessageBox.Show(Encoding.UTF8.GetString(sendB64));
}

PHP

if($link ->connect_errno) 
{
    echo 'ERROR: no connection!';
}
else 
{
    if(isset($_POST['b64string']))
    {
        $jsonstring = base64_decode($_POST['b64string']);

        $obj = json_decode($jsonstring);

        $query_opslaan = "INSERT INTO SalesKicker (BedrijfsNaam, ContPers, TelNr, Email, Land, Plaats, POC) VALUES ('". $obj->bedrijfsNaam ."' , '". $obj->ContPers ."', '". $obj->TelNum ."', '". $obj->email ."', '". $obj->Land ."', '". $obj->Plaats ."', '". $obj->PostCode ."')";         

        mysqli_query($link, $query_opslaan) or die(mysqli_error($query_opslaan));
    }
    else
    {
        echo 'ERROR: no data!';
    }

}

问题是我的PHP脚本返回

  

“错误:没有数据!”

这显然不会发生。应该发生的是PHP获取POST数据并将其保存到我的数据库。有人可以告诉我为什么这不能正常工作吗?

1 个答案:

答案 0 :(得分:0)

我应该使用以下c#方法,除了我当前的方法:

             var data = new NameValueCollection();
            data["b64string"] = b64encode;
            data["filename"] = dt.bedrijfsNaam;

            using (WebClient client = new WebClient())
            {
                var uploadData = client.UploadValues("http://" + ConfigurationManager.AppSettings["scripturi"].ToString() + "SalesKicker.php", "POST",
                data);
                response = Encoding.UTF8.GetString(uploadData);
            }

我应该一直使用NameValueCollection而不只是一个字符串。