0

我有兴趣将一些数据从我的 C# 代码发送到一个 php 页面,我已经可以发送一个变量甚至两个,但是当涉及到一个二维数组时,我已经一无所知了。

string postData我的目标是将参数中的 4 个二维数组通过所以我可以在 php 文件中处理它们,

这是我的代码:(这是我知道的用于 HTTP 通信的唯一方法)

private String communicateToServer(String serverHostname, String[,] disk = null, 
                                        String[,] hdd = null, String[,] nic = null, String[,] ram = null)
    {


        try
        {

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "user=" + SystemInformation.ComputerName; // user = a string containing the hostname
            byte[] data = encoding.GetBytes(postData);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverHostname); // its a link to a page
            request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            stream = response.GetResponseStream();

            StreamReader sr = new StreamReader(stream);

            String returnValue = sr.ReadToEnd();
            sr.Close();
            stream.Close();

            return returnValue;

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message);
        }

        return " ";

    }

谢谢你,祝你有美好的一天=)

4

1 回答 1

0

我将我的数组变成了一个大的 XML 字符串,然后将它作为单个字符串传递给这个函数,并将这个值用作发布数据:

我的论点:

private String communicateToServer(String serverHostname, String disk = null, String hdd = null,String nic = null, String ram = null)

我的帖子数据如何:

  if (disk == null && hdd == null && nic == null && ram == null)
  {
      postData = "user=" + SystemInformation.ComputerName; // user = a string containing the hostname
  }
  else
  {
      postData = "user=" + SystemInformation.ComputerName + "&disk=" + disk + "&hdd=" + hdd + "&nic=" + nic + "&ram" + ram;
  }

disk、hdd、nic、ram 的字符串值(示例):

<hddInfo>
<hddInterface>
    <model>TOSHIBA MQ01ABD075</model>
    <interfaceType>IDE</interfaceType>
    <name>\\.\PHYSICALDRIVE0</name>
    <partitions>3</partitions>
    <serialNumber>          X 52F801S4</serialNumber>
    <status>OK</status>
</hddInterface>
<hddInterface>
    <model>SD Card</model>
    <interfaceType>USB</interfaceType>
    <name>\\.\PHYSICALDRIVE1</name>
    <partitions>1</partitions>
    <serialNumber></serialNumber>
    <status>OK</status>
</hddInterface>

于 2014-03-01T06:51:06.360 回答