简体   繁体   English

如何在http get请求中发送数组

[英]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? 如何使用httpget方法发送数组? 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. 我需要在数组中发送它并在api,php中检索它,以便我可以将它插入到一行的数据库中。 currently, it's 1 data for 1 row. 目前,它是1行的1个数据。

you should passing 'value0' become just one querystring, ex : value[] 你应该传递'value0'变成一个查询字符串,例如: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 或者您应该将http_build_query用于param数组数据

the code you show is php ?? 你展示的代码是PHP? the api maybe php but the code style more like .NET c# correct me if im wrong. api也许是php,但代码风格更像.NET c#纠正我,如果我错了。

//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 这是一个在PHP中将数组转换为json的示例

$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() 现在你可以发送$json_arr然后你可以使用json_decode()解码它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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