因此,我到处搜寻,甚至在附近死去的官方论坛上询问。我已经在这里搜索过,但回复已经存在了多年,并且经常包含无效链接。
我只是想打开我家的灯。我已经通过调试工具进行了身份验证,并获得了“用户名”,然后将其硬编码到该应用程序中。同样,这只是我对其进行测试。我什至将功能性URL(http://192.168.0.100/api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights/2/)复制到其他浏览器和设备中,以确保无需在其他设备上通过重新认证进行处理。 是的,我也知道我正在从同步转移到异步,但是除非那是问题,否则我不担心将程序挂在那里。我只是想在API中进行切换:)
所以问题在于响应只是一个普通的HTTP 200 OK响应,而不是预期的API响应。
我得到:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Server: nginx
Date: Sun, 23 Sep 2018 18:37:44 GMT
Connection: close
Cache-Control: no-store, must-revalidate, no-cache, post-check=0, pre-check=0
Pragma: no-cache
Access-Control-Max-Age: 3600
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Headers: Content-Type
Content-Type: application/json
Expires: Mon, 01 Aug 2011 09:00:00 GMT
}
我期望什么时候
{"success":{"/lights/1/state/on":false}},
这是代码。有人可以对此有所启发吗?谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
namespace ConsoleApp1
{
class Program
{
static HttpClient client = new HttpClient();
static HttpResponseMessage response = new HttpResponseMessage();
public class StateO
{
public bool On { get; set; }
public int Bri { get; set; }
}
public class Light
{
public string Name { get; set; }
public StateO State { get; set; }
public Light()
{
State = new StateO();
}
}
static void Main(string[] args)
{
client.BaseAddress = new Uri("http://192.168.0.100/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
UpdateProductAsync().GetAwaiter().GetResult();
}
public static async Task<Light> UpdateProductAsync()
{
Light light = new Light();
light.State.On = false;
string json = JsonConvert.SerializeObject(light);
response = await client.PutAsJsonAsync(
$"api/RjplsYoXQvdTl11DOVIo92SKNB7vYRfwZvqCzvDK/lights/2/", json);
Console.WriteLine("potato: " + response.ToString());
response.EnsureSuccessStatusCode();
// light = await response.Content.ReadAsAsync<Light>();
return light;
}
}
}
答案 0 :(得分:1)
该IP地址是否适合您的网桥?您的响应标头来自Nginx服务器。
似乎您对Hue API端点有点困惑。
PUT
对/api/<username>/lights/<id>
的请求是重命名灯光
PUT
对/api/<username>/lights/<id>/state
的请求是更改灯光状态
文档为here