如何在放置后从飞利浦 Hue 桥接器获取 http json 响应

本文关键字:桥接 获取 http 响应 json Hue 飞利浦 | 更新日期: 2023-09-27 18:35:32

我正在尝试在完成 PUT 请求后从飞利浦色调中获取 json 响应。飞利浦 hue 在 Web 服务器命令行中内置,当我使用 hue Web 界面发送像"{"on":True} 这样的基本 json 消息时,我会收到一个 json 响应,告诉我请求成功。消息如下所示:

"[
    {
        "success": {
            "/lights/1/state/on": true
        }
    }
]"

现在,当我通过下面的设置向色调发送命令时,我总是得到一个 TRUE .IsSuccessStatusCode(我假设是因为消息是由色调接收的?)即使我发送了一些应该导致错误的json。例;我可以使用 json {"on":导致错误} 并且仍然得到一个 TRUE。

所以我试图弄清楚如何在发送 PUT 后从色调中获取上述 json 响应。下面是我正在使用的代码。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

    async static void setLight()
    {
        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage();
        String myJson = "{'"on'":false}";
        var content = new StringContent(myJson, Encoding.UTF8, "application/json");
        HttpResponseMessage returnStatement = await client.PutAsync("http://192.168.1.3/api/139f12ce32a30c473368dbe25f6586b/lights/1/state", content);
        Console.WriteLine(returnStatement.IsSuccessStatusCode);
    }

如何在放置后从飞利浦 Hue 桥接器获取 http json 响应

 Console.WriteLine("Return Statment:" + await returnStatement.Content.ReadAsStringAsync());

这将返回我正在寻找的 json 色调响应。我只是不断获取基本的http信息,直到我将await关键字放在它前面。向Q42.hueApi大喊大叫,我对你的代码进行了高峰,这有很大帮助。