我要做的是让我的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)??
非常感谢任何帮助!
答案 0 :(得分:9)
使用此代码从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");
}
和php页面
<?php
if (isset($_POST['message']))
{
$msg = $_POST['message'];
echo $msg;
}
?>
答案 1 :(得分: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);
}
2) 在您的PHP页面中:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
$postData = file_get_contents('php://input');
print $postData;
}
阅读有关在PHP中阅读帖子数据的信息: