简体   繁体   中英

POST data from c# to PHP and echo it

I know there are a lot of threads about this, but I have a little bit different question.

My code in c# is this:

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

And I want to see the $_POST array in php, so I tried using this code:

<?php
if (isset($_POST))
{
var_dump($_POST);
echo "<html><br/></html>";

print_r($_POST);
echo "<html><br/></html>";

print($_POST["message"]);
echo "<html><br/></html>";

$pst = file_get_contents('php://input');
var_dump($pst);
echo "<html><br/></html>";
}
?>

But I always get nothing inside the $_POST array. When I try using Microsoft Network Monitor, I see that the $_POST array does get inside it the data: $ _POST里面有数据

And just to be clear what I'm getting on the actual page: 在此处输入图片说明

I think it's somehow related to when I load the page... Don't know...

Thank you for any kind of help

This is not an complete answer, but I'm sending it here because it is too large for a comment.

I added these codes to the C# code, to get and print the response.

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

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

And I got this in the console:

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

And as you see, everything seems to be OK. So what is your problem exactly?

In a little bit delay (few days delay, I solved it at the day I posted this question). Well.. The solution is actually like @Ahmad said, something like a "bridge" page that gets the data and handles it. This is the Solution:

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

and last one is the index.php which only prints (for me) the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM