将字符串发送到PHP页面并使PHP页面显示字符串
我想要做的是让我的PHP页面显示一个字符串,我通过System.Net.WebClient通过我的C#应用程序中的函数创建。
真的是这样。 以最简单的forms,我有:
WebClient客户端=新WebClient(); string URL =“http://wwww.blah.com/page.php”; string TestData =“wooooo!test !!”; byte [] SendData = client.UploadString(URL,“POST”,TestData);
所以,我甚至不确定这是否是正确的方法..而且我不确定如何实际获取该字符串并将其显示在PHP页面上。 像print_r(SendData)?
任何帮助将不胜感激!
发布有两半。 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中阅读发布数据的信息:
使用此代码通过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"); }
和PHP页面