I will add additional information:
The best way to use Web Api in UWP is to use HttpClient, as mentioned earlier.
Here are a few examples that I think might be useful.
Itβs good practice to create a MobileServiceClient class, where you can collect all the operations that you can perform at the Web Api level:
public class MobileServiceClient { //access token if you use authentication: private string _accessToken; //address of your Web Api: private string _serviceAddress; //Constructor: public MobileServiceClient(string accessToken, string serviceAddress) { _accessToken = accessToken; _serviceAddress = serviceAddress; } //Now you can implement methods that you will invoke to perform selected operation related with Web Api: #region Methods //You can perform "Get" to retrieve object from the Web Api and then deserialize it (using Json .NET): public async Task<SampleClass> GetSampleClass() { SampleClass sampleClass= null; try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var data = await client.GetAsync(string.Concat(_serviceAddress, "routeName")); var jsonResponse = await data.Content.ReadAsStringAsync(); if (jsonResponse != null) sampleClass= JsonConvert.DeserializeObject<SampleClass>(jsonResponse); return sampleClass; } } catch (WebException exception) { throw new WebException("An error has occurred while calling GetSampleClass method: " + exception.Message); } } //You can perform "Get" to retrieve list of objects and then deserialize it: public async Task<List<SampleClass>> GetSampleClassObjects() { List<SampleClass> SampleClassObjectsList = null; try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var data = await client.GetAsync(string.Concat(_serviceAddress, "routeName")); var jsonResponse = await data.Content.ReadAsStringAsync(); if (jsonResponse != null) SampleClassObjectsList = JsonConvert.DeserializeObject<List<SampleClass>>(jsonResponse); return SampleClassObjectsList; } } catch (WebException exception) { throw new WebException("An error has occurred while calling GetSampleClassObjects method: " + exception.Message); } } //You can also "Post" some object: public async Task<bool> PostSomeObject(SampleClass sampleClassObject) { try { using (HttpClient client = new HttpClient()) { var sampleClassObjectJson = JsonConvert.SerializeObject(sampleClassObject); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var content = new StringContent(sampleClassObjectJson, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(string.Concat(_serviceAddress + "routeName"), content); if (response.StatusCode == HttpStatusCode.OK) return true; else throw new WebException("An error has occurred while calling PostSomeObject method: " + response.Content); } } catch (WebException exception) { throw new WebException("An error has occurred while calling PostFeedback method: " + exception.Message); } } #endregion }
Please note that with this implementation, you can share this code in the future if, for example, you decide to support other platforms (for example, Xamarin Android or iOS). Hope this helps you.
Daniel Krzyczkowski
source share