未经授权从Windows 10 IoT核心调用REST api事件中心时出错 [英] Unauthorized Error calling REST api event hub from Windows 10 IoT core

查看:86
本文介绍了未经授权从Windows 10 IoT核心调用REST api事件中心时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Windows 10 Iot Core调用PostAsync时,它会因未经授权而失败,但同样的代码工作正常从 开始我的笔记本电脑。

When I call PostAsync from Windows 10 Iot Core it fails with Unauthorized however same code works fine when i run from  my lap top.

请帮忙!

谢谢,

Pawan Shukla

Pawan Shukla

Pawan Shukla {博客:http://beingdeveloper.blogspot.in/}

Pawan Shukla { Blog: http://beingdeveloper.blogspot.in/ }

推荐答案

我可以确认下面的代码适用于Pi,10240或10586,如果有帮助的话

Well I can confirm that the below code works on the Pi, 10240 or 10586 if that helps

//using System.Net.Http;
using Windows.Web.Http;
...
...
            // Create a New HttpClient object and dispose of it when done.
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");

                // Call asynchronous network methods in a try/catch block to handle exceptions
                try
                {
                    //HttpStringContent stringContent = new HttpStringContent("test data");
                    //HttpStreamContent streamContent = new HttpStreamContent(new InMemoryRandomAccessStream());
                    //HttpBufferContent bufferContent = new HttpBufferContent(new Windows.Storage.Streams.Buffer(100));

                    Uri uri = new Uri("http://{myRESTserver}/{myRESTservice}?{parameters}");
                    HttpResponseMessage response = await httpClient.PostAsync(uri, null); 
                    // or add content (see above) as required in place of the null
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    // TODO: Do something with the responseBody
                }
                catch (Exception ex)
                {
                    // TODO: Deal with exception - could be a server not found, 401, 404, etc.
                }
            }


这篇关于未经授权从Windows 10 IoT核心调用REST api事件中心时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆