将字符串发送到PHP页面并使PHP页面显示字符串

kog*_*ogh 2 php c# webclient

我想要做的是让我的PHP页面显示一个字符串,我通过System.Net.WebClient通过我的C#应用​​程序中的函数创建.

真的是这样的.以最简单的形式,我有:

WebClient client = new WebClient();  
string URL = "http://wwww.blah.com/page.php";
string TestData = "wooooo! test!!";

byte[] SendData = client.UploadString(URL, "POST", TestData);

所以,我甚至不确定这是否是正确的方法..而且我不确定如何实际获取该字符串并将其显示在PHP页面上.像print_r(SendData)?

任何帮助将不胜感激!

Pou*_*abi 9

使用此代码通过Post方法从C#发送字符串

       try
       {
            string url = "";
            string str = "test";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            string Data = "message="+str;
            byte[] postBytes = Encoding.ASCII.GetBytes(Data);
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream resStream = response.GetResponseStream();

            var sr = new StreamReader(response.GetResponseStream());
            string responseText = sr.ReadToEnd();


        }
        catch (WebException)
        {

            MessageBox.Show("Please Check Your Internet Connection");
        }
Run Code Online (Sandbox Code Playgroud)

和PHP页面

 <?php 
    if (isset($_POST['message']))
    {
        $msg = $_POST['message'];

        echo $msg;

    }

   ?>
Run Code Online (Sandbox Code Playgroud)


Use*_*ser 6

发布有两个部分.1)发布到页面的代码和2)接收它的页面.

1)你的C#看起来不错.我个人会用:

string url = "http://wwww.blah.com/page.php";
string data = "wooooo! test!!";

using(WebClient client = new WebClient()) {
    client.UploadString(url, data);  
}
Run Code Online (Sandbox Code Playgroud)

对于2)在您的PHP页面中:

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
    $postData = file_get_contents('php://input');
    print $postData;
}
Run Code Online (Sandbox Code Playgroud)

阅读有关在PHP中阅读帖子数据的信息: