public void Run() { string content = string.Empty; string catUrl = "https://cat-fact.herokuapp.com/facts/random"; CatFactModel catFact = new CatFactModel(); try { using (var wc = GetWebClient()) { content = wc.DownloadString(catUrl); } catFact = JsonSerializer.Deserialize <CatFactModel>(content); } catch { Console.WriteLine("Got no result or couldn't convert to a cat fact"); return; } Console.WriteLine(); if (catFact.Status != null && catFact.Status.Verified) { Console.ForegroundColor = ConsoleColor.Yellow; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(catFact.Text); Console.WriteLine(); }
static void CatFact() { string content = string.Empty; string catUrl = "https://cat-fact.herokuapp.com/facts/random"; using (var wc = new WebClient()) { content = wc.DownloadString(catUrl); } CatFactModel catFact = JsonSerializer.Deserialize<CatFactModel>(content); Console.WriteLine(); if (catFact.Status != null && catFact.Status.Verified) Console.ForegroundColor = ConsoleColor.Yellow; else Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(catFact.Text); Console.WriteLine(); }
private static void CatFact() { logger.LogInformation($"User Choice : {nameof(CatFact)}"); string content = string.Empty; string catUrl = "https://cat-fact.herokuapp.com/facts/random"; using (var wc = new WebClient()) { content = wc.DownloadString(catUrl); } CatFactModel catFact = JsonSerializer.Deserialize <CatFactModel>(content); Console.WriteLine(); if (catFact.Status != null && catFact.Status.Verified) { Console.ForegroundColor = ConsoleColor.Yellow; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(catFact.Text); Console.WriteLine(); }
static void CatFact() { string content = string.Empty; string catUrl = "https://cat-fact.herokuapp.com/facts/random"; WebRequest catReq = WebRequest.Create(catUrl); using (WebResponse wr = catReq.GetResponse()) using (Stream receiveStream = wr.GetResponseStream()) using (StreamReader sReader = new StreamReader(receiveStream, Encoding.UTF8)) content = sReader.ReadToEnd(); CatFactModel catFact = JsonSerializer.Deserialize <CatFactModel>(content); Console.WriteLine(""); if (catFact.status.verified) { Console.ForegroundColor = ConsoleColor.Yellow; } else { Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(catFact.text); Console.WriteLine(""); }