Gk_999
Gk_999

Reputation: 518

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??

Here is snapshot of app capabilities

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

Upvotes: 0

Views: 376

Answers (1)

Dani
Dani

Reputation: 1047

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.

Upvotes: 1

Related Questions