Ejemplo n.º 1
0
        /// <summary>
        /// Obtiene todos los heroes enviando un request GET.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async void AgregarHero(object sender, EventArgs e)
        {
            var form = new FormNuevoHeroe();

            if (form.ShowDialog() == DialogResult.OK)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(DIRECCION_SERVIDOR);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APP_JSON));

                    Hero heroe = new Hero {
                        Name = form.DarNombre(), Species = form.DarEspecie(), Type = form.DarTipo(), World = form.DarMundo()
                    };
                    HttpResponseMessage response = await client.PostAsJsonAsync <Hero>(RUTA_HEROES, heroe);

                    if (response.IsSuccessStatusCode)
                    {
                        heroe = await response.Content.ReadAsAsync <Hero>();

                        AgregarHeroControl(heroe);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private async void buttonVer_Click(object sender, EventArgs e)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(Form1.DIRECCION_SERVIDOR);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Form1.APP_JSON));

                HttpResponseMessage response = await client.GetAsync(Form1.RUTA_HEROES + "/" + Hero.Id);

                if (response.IsSuccessStatusCode)
                {
                    Hero heroe = await response.Content.ReadAsAsync <Hero>();

                    var dialogo = new FormNuevoHeroe();
                    dialogo.CambiarNombre(heroe.Name);
                    dialogo.CambiarTipo(heroe.Type);
                    dialogo.CambiarEspecie(heroe.Species);
                    dialogo.CambiarMundo(heroe.World);
                    dialogo.ShowDialog();
                }
            }
        }