0
c# using httpclient
using System; using System.Net.Http; class Program { public static void Main (string[] args) { string GetRandomWord() { HttpClient wc = new HttpClient(); string wordlist = wc.DownLoadString("https://raw.githubusercontent.com/imsky/wordlists/master/nouns/food.txt"); string[] words = wordlist.Split('\n'); Random rnd = new Random(); return words[rnd.Next(0, words.Length - 1)]; } } } the code is supposed to run a random word selected in the link however it's not running I believe it has something to do with httpclient.
1 Réponse
0
Hi, I see you declared the GetRandomWord method inside the Main method so GetRandomWord is never called. Try using async methods aswell, here is the code :
static async Task Main(string[] args)
{
HttpClient wc = new HttpClient();
using (HttpResponseMessage response = await wc.GetAsync("https://raw.githubusercontent.com/imsky/wordlists/master/nouns/food.txt"))
{
if (response.IsSuccessStatusCode)
{
string wordlist = await response.Content.ReadAsStringAsync();
string[] words = wordlist.Split('\n');
Random rnd = new Random();
Console.WriteLine( words[rnd.Next(0, words.Length - 1)]);
}
}
Console.ReadLine();
}