【问题标题】:UWP app with restful PUT带有宁静 PUT 的 UWP 应用
【发布时间】:2018-12-30 19:08:39
【问题描述】:

我对 C# 非常陌生,但我需要编写一个 UWP 应用程序才能在具有 Windows 10 IoT 的 Raspberry Pi 上运行。

我有一个连接到 Pi 的蓝牙加密狗,当按下按钮时,应用程序需要向 Philipps Hue Ligthscrip 发送一个 On-Command 并带有一个安静的 PUT。

我曾尝试通过 Stack Overflow、Microsoft 的存储库和数以千计的其他网站搜索自己,但没有成功。 HttpClient (https://docs.microsoft.com/en-us/windows/uwp/networking/httpclient) 一定有可能,但我不知道如何发送 PUT...

谢谢

多米尼克

【问题讨论】:

  • 那么到底是什么不工作?你有写过的代码吗?
  • 谢谢@p3tch,不幸的是没有。我不知道从哪里开始。有什么提示吗?
  • 你看过这个吗? developers.meethue.com/documentation/getting-started 那和微软关于 HTTPClient 的文档至少应该是一个起点
  • 抱歉,我需要发送 PUT 而不是 GET。不幸的是微软没有描述如何使用 PUT...
  • HttpClient 有PutAsync(),你可以等待,或者你可以使用HttpMethod.Put 建立一个HttpRequestMessage 并将其传递给HttpClientSendAsync() 方法

标签: c# uwp raspberry-pi3 windows-10-iot-core


【解决方案1】:

在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2015-07-19
    • 2018-04-21
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多