Jump to Navigation

C# 调用RESTFul接口发送指令

SR-201系列产品的云遥控二次开发包提供RESTful webservice接口控制模块,提供GetDeviceStatus和SendCommand2个URL。

POST device ID 和 device password 到以上URL,注意 Content-Type: text/xml

如:<DeviceQuery><devId>00000000000000</devId><devPwd>000000</devPwd></DeviceQuery>

POST device ID 、device password 和 command  到以上URL,注意 Content-Type: text/xml

如:<DeviceQuery><devId>00000000000000</devId><devPwd>000000</devPwd><devCmd>11</devCmd></DeviceQuery>

 

public int SendCommand(string server, string devId, string devPwd, string cmd)
{
    Uri address = new Uri(string.Format("http://{0}/SyncServiceImpl.svc/SendCommand", server));///转换为uri,加以标记
    string receiveText = "";///用来存储返回结果

    // 创建webrequest
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

    // Set type to POST
    request.Method = "POST";
    request.ContentType = "text/xml";
    string sendText = string.Format("{0}{1}{2}", devId, devPwd, cmd);

    // 设置请求头的长度
    request.ContentLength = sendText.Length;

    // 写入待请求的参数
    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(Encoding.ASCII.GetBytes(sendText), 0, sendText.Length);
    }

    try
    {
        // 获取响应
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //  获取响应数据流
            StreamReader reader = new StreamReader(response.GetResponseStream());

            //把响应后的数据从头读取到尾
            receiveText = reader.ReadToEnd();
            XDocument xmlDoc = XDocument.Parse(receiveText);
            return Convert.ToInt16(xmlDoc.Root.Value);
        }
    }
    catch (Exception e)
    {
        throw;
    }
}


Main menu 2

about seo