public ModelUsuario ObtenerUnUsuario(string id) { try { ModelUsuario usuario = null; using (var client = new HttpClient()) { var task = Task.Run(async() => { return(await client.GetAsync(URL_API + "/UnEstudiante?id=" + id)); } ); HttpResponseMessage message = task.Result; if (message.StatusCode == System.Net.HttpStatusCode.InternalServerError) { var task1 = Task <string> .Run(async() => { return(await message.Content.ReadAsStringAsync()); }); string mens = task1.Result; System.Web.Mvc.ModelError error = JsonConvert.DeserializeObject <System.Web.Mvc.ModelError>(mens); return(usuario); } else if (message.StatusCode == System.Net.HttpStatusCode.NotFound) { var task2 = Task <string> .Run(async() => { return(await message.Content.ReadAsStringAsync()); }); string mens = task2.Result; return(usuario); } else { var task2 = Task <string> .Run(async() => { return(await message.Content.ReadAsStringAsync()); }); string mens = task2.Result; usuario = JsonConvert.DeserializeObject <ModelUsuario>(mens); } return(usuario); } } catch (Exception ex) { throw ex; } }
public List <ModelUsuario> ConnectGET() { try { List <ModelUsuario> lista = null; using (var client = new HttpClient()) { var task = Task.Run(async() => { return(await client.GetAsync(URL_API)); } ); HttpResponseMessage message = task.Result; if (message.StatusCode == System.Net.HttpStatusCode.InternalServerError) { var task1 = Task <string> .Run(async() => { return(await message.Content.ReadAsStringAsync()); }); string mens = task1.Result; System.Web.Mvc.ModelError error = JsonConvert.DeserializeObject <System.Web.Mvc.ModelError>(mens); return(lista); } else { var task2 = Task <string> .Run(async() => { return(await message.Content.ReadAsStringAsync()); }); string mens = task2.Result; lista = JsonConvert.DeserializeObject <List <ModelUsuario> >(mens); } return(lista); } } catch (Exception ex) { throw ex; } }