将数据从c#发布到PHP并回显它

时间:2015-09-18 14:44:57

标签: c# php

我知道有很多线索,但我有一点不同的问题。

我在c#中的代码是这样的:

Calendar.FindAppointments()

我想在php中看到$ _POST数组,所以我尝试使用这段代码:

            string url = "http://10.0.0.4/CS_PHP/index.php";

            byte[] byteArray = Encoding.UTF8.GetBytes("message=This is the message" + "&" + "username=This is the usrname");

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;

            using (Stream webpageStream = webRequest.GetRequestStream())
            {
                webpageStream.Write(byteArray, 0, byteArray.Length);
            }

但我总是在$ _POST数组中得不到任何东西。 当我尝试使用Microsoft网络监视器时,我看到$ _POST数组确实包含了数据: $_POST has the data inside it

要清楚我在实际页面上得到的内容: enter image description here

我认为这与我加载页面的方式有关...不知道......

感谢您提供任何帮助

2 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,但我在这里发送它是因为它太大而无法发表评论。

我将这些代码添加到C#代码中,以获取并打印响应。

var res = new byte[1000];
webRequest.GetResponse().GetResponseStream().Read(res, 0, 1000);

Console.WriteLine(ASCIIEncoding.ASCII.GetString(res));

我在控制台中得到了这个:

???array(2) {
  ["message"]=>
  string(19) "This is the message"
  ["username"]=>
  string(19) "This is the usrname"
}
<html><br/></html>Array
(
    [message] => This is the message
    [username] => This is the usrname
)
<html><br/></html>This is the message<html><br/></html>string(56) "message=This is the message&username=This is the usrn
ame"
<html><br/></html>

如你所见,一切似乎都没问题。那么你的问题到底是什么?

答案 1 :(得分:0)

稍微延迟(几天延迟,我在发布这个问题的那天解决了它)。嗯..解决方案实际上就像@Ahmad所说的那样,就像一个&#34; bridge&#34;获取数据并处理数据的页面。 这是解决方案:

<强> C#:

            string url = "http://10.0.0.4/cnc/cs_php/posting.php";

            byte[] byteArray = Encoding.UTF8.GetBytes("hostname=Nadav" + "&" + "ip=10.0.0.4" + "&" + "os=Windows 10 x64");

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;

            using (Stream webpageStream = webRequest.GetRequestStream())
            {
                webpageStream.Write(byteArray, 0, byteArray.Length);
            }

<强> posting.php

<?php
  $v1 = $_POST["value1"];
  $v2 = $_POST["value2"];
  $v3 = $_POST["value3"];

  $servername = "server name";
  $usernamedb = "DB username";
  $passworddb = "DB password";
  $dbname = "DB name";

  // Create connection
  $conn = new mysqli($servername, $usernamedb, $passworddb, $dbname);
 // Check connection
 if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 
 if (!empty($host))
 {
    $sql = "INSERT INTO..." //You know... The query of inserting into the table

 if ($conn->query($sql) === TRUE) 
 {
    header("location: index.php");
 }
 else 
 {
    echo "Error: " . $sql . "<br>" . $conn->error;
 }
}
$conn->close();
?>

,最后一个是index.php,它只打印(对我来说)表。