如何从WinRT / Store应用程序调用Web api休息服务

时间:2014-12-22 19:31:13

标签: c# asp.net-web-api windows-runtime windows-store-apps webservice-client

我有一个简单的要求:我需要安全地访问restfull Webapi,接收一些数据进行登录,然后返回一些数据:

我必须发送一些数据:

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

我希望返回一份约会名单:

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; }
}

如何在Windows 8.1 Store / WinRT应用程序中执行此操作?

1 个答案:

答案 0 :(得分:1)

尝试这种方法:

    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);
            }
        }
    }

作为json转换器,我使用Json.NET