【问题标题】:WinRT use HttpClient to call Web API based URL with tokenWinRT 使用 HttpClient 使用令牌调用基于 Web API 的 URL
【发布时间】:2014-11-12 00:35:23
【问题描述】:

我们正在构建一个 WinRT 应用程序,该应用程序从基于 Web API 的服务器获取数据,因此它以 json 和/或 XML 格式提供数据。

当应用用户第一次使用他的凭据(用户名,密码)登录时,来自服务器的响应是一个成功位和一个 TOKEN,应该在后续的 URL 请求中使用。

我正在使用 httpclient 发送请求

     using (HttpClient httpClient1 = new HttpClient())
                    {
                        string url = "http://example.com/abc/api/process1/GetLatestdata/10962f61-4865-4e7a-a121-3fdd968824b5?employeeid=6";

 //The string 10962f61-4865-4e7a-a121-3fdd968824b5 is the token sent by the server               

                        var response = await httpClient1.GetAsync(new Uri(url));

                        string content = await response.Content.ReadAsStringAsync();
                    }

现在我得到的响应是状态码 401“未经授权”。 我得到的响应是“未经授权的用户”。

我需要在 appManifest 中更改什么吗??

我检查了this,但我们不能在没有凭据的情况下使用 httpclient 吗??

【问题讨论】:

    标签: windows-runtime windows-store-apps winrt-xaml asp.net-web-api


    【解决方案1】:

    你的能力已经足够了。您甚至不需要 Internet(客户端),因为它包含在 Internet(客户端和服务器)中。

    您没有 WinRT HttpClient 的凭据,在您链接的帖子中,他们引用了 System.Net.Http.HttpClientHandler

    也许您可以使用HttpBaseProtocolFilter 添加凭据?

    using (var httpFilter = new HttpBaseProtocolFilter())
    {
        using (var httpClient = new HttpClient(httpFilter))
        {
            httpFilter.ServerCredential...
        }
    }
    

    我不知道您的安全机制,我使用的是HttpClient,而我的会话密钥在 cookie 中。但我认为您的客户端代码看起来不错。

    【讨论】:

    • Dani u r 正确...我的客户端代码很好...在验证请求时服务器端出现了一些问题...谢谢
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2020-02-19
    • 2019-03-13
    • 2020-03-02
    相关资源
    最近更新 更多