我正在尝试string[] str = { "abc" , "sdfsdf" };
使用下面的代码将此数组值发送到 PHP(Web 服务),但它总是给我以下输出
在 PHP 文件中,我有以下代码,它实际上接收数组并输出带有值的总结构:
<?php
$messages = $_POST['messages'];
print_r($messages);
?>
问题可能是 PHP 无法读取我发送的数组;可能是因为我是从 C# 发送的。
您能否告诉我如何正确发送数组,以便 PHP Web 服务可以读取它。
仅供参考:我无权在 Web 服务端编辑任何代码。
我的完整 C# 代码
string[] str = { "num" , "Hello World" };
string url = "http://localhost/a/cash.php";
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create( url );
ASCIIEncoding encoding = new ASCIIEncoding();
string postData ;//= "keyword=moneky";
postData = "&messages[]=" + str;
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
MessageBox.Show(responseString);