0

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

Fukkatsu
  • 149
  • 1
  • 12
  • You could basically implode/join the entire array into a single string, then in PHP explode it again ? The joining would need to be performed in C# of course. – Tularis Feb 27 '14 at 00:48
  • could be achieved with json since you're doing a webrequest, I think, and will be easier to parse from C# and PHP: http://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server – Allende Feb 27 '14 at 00:49
  • is it possible to send the whole 2d arrays instead? i'm going to process the data in the php file, -makes life easier- Thanks for the recommendation though – Fukkatsu Feb 27 '14 at 00:49
  • 1
    well, the way you're sending it now is via a HTTP request, which accepts only data as strings. So you're forced to first transform your array into a string just to be able to pass it this way. I'm sure there are ways to send it as a different type, but those would require active communication in different ways; i.e. via XML-RPC (which basically also is via requests, and thus via strings). – Tularis Feb 27 '14 at 00:54
  • 1
    When passing from a C# to a PHP page you're making an HTTP request. What can be transferred in an HTTP request with POST/GET? Key-value pairs of text string: nothing else. Whatever goes inside of these, will be received by the page. It is then your job to decode this strings and understand the data structure. I'm suggesting you to use JSON: it takes just 1 line of code in each page to encode/decode. – Saturnix Feb 27 '14 at 00:55
  • 2
    Just remember that a C# array is nothing like a PHP array. Both in its implementation and internally. – Tularis Feb 27 '14 at 00:55
  • 1
    ^ exactly. There's no such thing as transferring data structures between pages. You're just transferring plain text. Decoding/encoding of this plain text into variables/arrays/database entities/donuts is totally up to you. If PHP was to automagically transform GET/POST strings into arbitrary data structure I could potentially inject these with whatever I like without you ever noticing. – Saturnix Feb 27 '14 at 01:00
  • Thanks for all the help =) I Decided to parse the code to an XML string since i will do that anyway within my php file =) – Fukkatsu Feb 27 '14 at 01:31

1 Answers1

0

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>

Fukkatsu
  • 149
  • 1
  • 12