Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 845
user145610 Avatar asked Jan 11 '14 21:01

user145610


2 Answers

Demystifying HttpClient APIs in the Universal Windows Platform

Which one should I use?

Since both of these APIs are available in UWP, the biggest question for HTTP developers is which one to use in their app. The answer is that it depends on a couple of factors:

  1. Do you need to integrate with native UI for collecting user credentials, control HTTP cache read and write behavior; or pass in a specific SSL client certificate for authentication?

If yes – then use Windows.Web.Http.HttpClient. At the time of this writing, the Windows.Web.Http API provides greater control over HTTP settings in UWP than the System.Net.Http API. In future versions, the System.Net.Http API may also be enhanced to support these features on UWP.

  1. Do you intend to write cross-platform .NET code (across UWP/ASP.NET 5/iOS and Android)?

If yes – then use System.Net.Http API. This allows you to write code that you can re-use on other .NET platforms such as ASP.NET 5 and .NET Framework desktop applications. Thanks to Xamarin, this API is also supported on iOS and Android, so you can reuse your code on these platforms as well.

like image 63
Ðаn Avatar answered Sep 21 '22 13:09

Ðаn


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);
like image 40
kiewic Avatar answered Sep 17 '22 13:09

kiewic