6

以下のコードを使用して、この配列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);
4

3 に答える 3

3

多くの試行錯誤の末、私は最終的に正しい解決策を自分で見つけました。誰かが必要な場合のコードは次のとおりです。

私は辞書を使わなければなりませんでした:

Dictionary<string, string> myarray =
    new Dictionary<string, string>();

    myarray .Add("0", "Number1");
    myarray .Add("1", "Hello World");

その後

string str = string.Join(Environment.NewLine, myarray); 

次に、残りのコード:

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);
于 2013-10-06T15:12:04.623 に答える
1

C# コードを編集します。

古いコード

string postData ;//= "keyword=moneky";
postData = "&messages[]=" + str;

新しいコード

string postData = "";
foreach (string oneString in str) {
   postData += "messages[]=" + oneString + "&";
}
于 2013-10-06T14:39:05.180 に答える