当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net web api - WinRT use HttpClient to call Web API based URL with token

问题描述:

We are building a WinRT app which gets data from server which is Web API based & so it gives data in json and/or XML format.

When app user logs in for the first time using his credentials(username,password), the response that comes from server is a success bit & a TOKEN, which should be used in successive URL requests.

I am using httpclient for sending requests

 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();

}

Now the response that i get is with status code 401 "unauthorised".

And the xml i get in response is "Unauthorised User".

Is there anything i need to change in appManifest??

I've checked this, but cant we use httpclient without credentials??

网友答案:

Your Capabilities are enough. You don't even need Internet (Client) because it's included in Internet (Client & Server).

You do not have credentials for WinRT HttpClient, in your linked post they referr to System.Net.Http.HttpClientHandler.

Maybe you can use the HttpBaseProtocolFilter to add the credentials?

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

I don't know your security mechanism, I'm using a HttpClient and my session-key is in a cookie. But I think your client code looks fine.

分享给朋友:
您可能感兴趣的文章:
随机阅读: