RHAD
RHAD

Reputation: 1357

How to call a webapi rest service from WinRT / Store app

I have a simple requirement: I need to access a restfull Webapi securely that receives some data to logon and then returns some data:

I have to send some data:

public class CredentialsModel
{
    public string User { get; set; }
    public string Password { get; set; }
            public string ExchangeRoomId {get; set; }
}

And I want a list of Appointment to be returned:

public class Appointment
{
    public Guid AppointmentId { get; set; }
    public string AppointmentExchangeId { get; set; }

    public string Description { get; set; }

    public DateTime Start { get; set; }
    public DateTime End { get; set; }

    public TimeZoneInfo TimeZone { get; set; }
}

How can I do this in a Windows 8.1 Store / WinRT app?

Upvotes: 1

Views: 657

Answers (1)

Dani
Dani

Reputation: 1047

Try this approach:

    using (var httpFilter = new HttpBaseProtocolFilter())
    {
        using (var httpClient = new HttpClient(httpFilter))
        {
            Uri requestUri = new Uri("");
            string json = await JsonConvert.SerializeObjectAsync(CredentialsModel);
            var response = await httpClient.PostAsync(requestUri, new HttpStringContent(json, UnicodeEncoding.Utf8, "application/json"));
            if (response.StatusCode == HttpStatusCode.Ok)
            {
                var responseAsString = await response.Content.ReadAsStringAsync();
                var deserializedResponse = await JsonConvert.DeserializeObjectAsync<IEnumerable<Appointment>>(responseAsString);
            }
        }
    }

As the json converter I use Json.NET

Upvotes: 1

Related Questions