Search code examples
c#phparrayspostdata

How to send a 2D array in C# code to php page?


I am interested in sending some data from my C# code to a php page, I can already send a single variable and even two, but when it comes to a 2D array i'm already clueless.

My goal is to send the 4 2D arrays within the arguments to a php page via the string postData and echo back the data into one really long string (I can handle the rest when I'm able to do this) I need to send the 2D arrays so i can process them in the php file,

Here is my code: (This is the only method i know for HTTP communication)

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 " ";

    }

Thank you and have a nice day =)


Solution

  • I turned My array into a big XML string then passed it to this function as a single string and used this value as post data:

    My Arguments:

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

    How my Post Data looks like:

      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;
      }
    

    String values for disk,hdd,nic,ram (example):

    <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>