我是C#的新手,但我需要编写一个UWP应用程序才能在具有Windows 10 IoT的Raspberry Pi上运行。
我有一个连接到Pi的蓝牙加密狗,当按下按钮时,应用程序需要通过一个安静的PUT向Philipps Hue Ligthscrip发送一个On-Command。
我已经尝试通过Stack Overflow,Microsoft的存储库以及成千上万个其他站点来向Google展示自己,但是没有运气。 HttpClient(https://docs.microsoft.com/en-us/windows/uwp/networking/httpclient)一定有可能,但我不知道如何发送PUT ...
谢谢
多米尼克
答案 0 :(得分:0)
在C#中的UWP中,您可以使用httpClient.PutAsync()或httpClient.SendRequestAsync()API发送PUT请求。以下是使用这两个API创建新设备的简单示例。
使用PutAsync():
Uri requestUri = new Uri("http://sample-server-address/devices/newdevice");
string content = @"{ deviceId:'newdevice'}";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PutAsync(requestUri, new HttpStringContent(content));
使用SendRequestAsync():
Uri requestUri = new Uri("http://sample-server-address/devices/newdevice");
string content = @"{ deviceId:'newdevice'}";
Windows.Web.Http.HttpStringContent requestBody = new HttpStringContent(content, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpRequestMessage message = new HttpRequestMessage();
message.RequestUri = requestUri;
message.Method = HttpMethod.Put;
message.Content = requestBody;
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendRequestAsync(message);
参考“ UWP networking Windows.Web.Http.HttpClient”“ HttpClient sample(UWP)”