Search code examples
asp.net-web-apiwindows-store-appshttpclientwindows-8.1

Confuse in using which version of HttpClient in Windows.Web.Http and System.Net.Http;


I'm new to Web API and Windows Store App 8.1. I'm developing a Windows Store app that communicates to Web API. When I try to write the following code:

// server:53452/api/demo?ReportingMonth=10&ReportingYear=2013" 

using (HttpClient httpClient = new HttpClient())
        {
            using (HttpResponseMessage response = await httpClient.GetAsync(new Uri("address")))
            {
                string result = await response.Content.ReadAsStringAsync();
                var prodt = JsonConvert.DeserializeObject<ObservableCollection<Statuses>>(result);
                return prodt;
            }
        }

I see that HttpClient is in both Windows.Web.Http and System.Net.Http. Which namespace should I use?

If I pick the System.Net.Http namespace, when I try to call my Web API, which is Windows Authenticate enabled, the cursor will not return back to the client, remaining in unknown state. Not sure about why I'm not receiving the response.

address = "abc.com:53452/api/demo?ReportingMonth=10&ReportingYear=2013"
using (HttpResponseMessage response = await httpClient.GetAsync(new Uri(address)))

If i use HttpClient from the Windows.Web.Http, windows store app asks me to enter credentials, and even though I entered my credentials correctly, system keeps prompting to enter the credentials. Can anyone explain why that happens?


Solution

  • To perform HTTP authentication, instead of HttpClientHandler use HttpBaseProtocolFilter:

    var filter = new HttpBaseProtocolFilter();
    filter.ServerCredential = new 
        Windows.Security.Credentials.PasswordCredential(uri.ToString(), "foo", "bar");
    var client = new HttpClient(filter);