Saltar al contenido

cómo consumir rest api en el ejemplo de código c #

Te damos la bienvenida a nuestra web, aquí vas a hallar la respuesta de lo que estabas buscando.

Ejemplo 1: c # make request to rest api

usingSystem;usingSystem.Collections.Generic;usingSystem.Net.Http;usingSystem.Net.Http.Headers;namespaceConsoleProgrampublicclassDataObjectpublicstring Name get;set;publicclassClass1privateconststring URL ="https://sub.domain.com/objects.json";privatestring urlParameters ="?api_key=123";staticvoidMain(string[] args)HttpClient client =newHttpClient();
            client.BaseAddress =newUri(URL);// Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));// List data response.HttpResponseMessage response = client.GetAsync(urlParameters).Result;// Blocking call! Program will wait here until a response is received or a timeout occurs.if(response.IsSuccessStatusCode)// Parse the response body.var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;//Make sure to add a reference to System.Net.Http.Formatting.dllforeach(var d in dataObjects)
                    Console.WriteLine("0", d.Name);else
                Console.WriteLine("0 (1)",(int)response.StatusCode, response.ReasonPhrase);//Make any other calls using HttpClient here.//Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();

Ejemplo 2: servicio posterior al descanso que consume c #

usingSystem;usingSystem.Collections.Generic;usingSystem.Net.Http;usingSystem.Net.Http.Headers;namespaceConsoleProgrampublicclassDataObjectpublicstring Name get;set;publicclassClass1privateconststring URL ="https://sub.domain.com/objects.json";privatestring urlParameters ="?api_key=123";staticvoidMain(string[] args)HttpClient client =newHttpClient();
            client.BaseAddress =newUri(URL);// Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));// List data response.HttpResponseMessage response = client.GetAsync(urlParameters).Result;// Blocking call! Program will wait here until a response is received or a timeout occurs.if(response.IsSuccessStatusCode)// Parse the response body.var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;//Make sure to add a reference to System.Net.Http.Formatting.dllforeach(var d in dataObjects)
                    Console.WriteLine("0", d.Name);else
                Console.WriteLine("0 (1)",(int)response.StatusCode, response.ReasonPhrase);//Make any other calls using HttpClient here.//Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *