简体   繁体   中英

How to send array in http get request

private void timer1_Tick(object sender, System.EventArgs e)
    {
        int iIdx;
        int[] iData;
        bool[] bData;


        if (m_bRegister) // Read registers (4X references)
        {
            // read register (4X) data from slave
            if (adamTCP.Modbus().ReadHoldingRegs(m_iStart, m_iLength, out iData))
            {
                m_iCount++; // increment the reading counter
                txtStatus.Text = "Read registers " + m_iCount.ToString() + " times...";
                // update ListView

                label1.Text = HttpGet("http://127.0.0.1/misc/api1.php?value0=" + iData[0].ToString());
                label2.Text = HttpGet("http://127.0.0.1/misc/api1.php?value1=" + iData[1].ToString());
                label3.Text = HttpGet("http://127.0.0.1/misc/api1.php?value2=" + iData[2].ToString());
                label4.Text = HttpGet("http://127.0.0.1/misc/api1.php?value3=" + iData[3].ToString());
                label5.Text = HttpGet("http://127.0.0.1/misc/api1.php?value4=" + iData[4].ToString());

                for (iIdx = 0; iIdx < m_iLength; iIdx++)
                {
                    listViewModbusCur.Items[iIdx].SubItems[2].Text = iData[iIdx].ToString();            // show value in decimal
                    listViewModbusCur.Items[iIdx].SubItems[3].Text = iData[iIdx].ToString("X04");   // show value in hexdecimal

                }

How do i send array using httpget method? The code on top show that i'm sending data one by one. I need to send it in array and retrieve it back in api,php so that i could insert it into database in one row. currently, it's 1 data for 1 row.

you should passing 'value0' become just one querystring, ex : value[]

so the url will become

http://127.0.0.1/misc/api1.php?value[]="+ iData[0].ToString()"&value[]="+ iData[1].ToString() and etc what you want to send it " 

or you should use http_build_query to the param array data

the code you show is php ?? the api maybe php but the code style more like .NET c# correct me if im wrong.

//Update Response

string[] param = new string[(how many your counter param)]; 
param [0] = "test0"; 
param [1] = "test1"; 
param [2] = "test2"; 

in my example i give you 3
string sParams = JsonConvert.SerializeObject(param) ;
HttpGet("http://127.0.0.1/misc/api1.php?value=" + sParams);

Here is a example to convert array to json in PHP

$arr=array(
    "varl1"=>1,
    "varl2"=>"example",
    "varl3"=>3,
 );

$json_arr=json_encode($arr);

Now you can send $json_arr and then you can decode it using json_decode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM