[C#] Consumir una API REST con WebClient
Código para obtener la lista de valores UF en el año actual desde la página www.mindicador.cl, el arreglo se presenta en formato JSON y se consume directamente sin login ni cambio de verbo http.
public string apiUF()
{
string url = "http://www.mindicador.cl/api/uf/" + DateTime.Now.Year.ToString();
string res = "", fecha = "", valor = "" ;
try
{
WebClient web = new WebClient();
System.IO.Stream stream = web.OpenRead(url);
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
res = reader.ReadToEnd();
}
ObjectoJson objSerie = JsonConvert.DeserializeObject(res);
foreach (var item in objSerie.serie)
{
fecha = item.fecha.ToString("dd/MM/yyyy");
valor = item.valor.ToString().Replace(",", ".");
}
}
catch (Exception ex)
{
return ex.ToString();
}
return res;
}