C#C
C#3y ago
rallez

✅ Basic http client get request

Hi why my code won't return any value
static async Task<int> GetServerTime()
{
    using (HttpClient client = new HttpClient())
    {
        try
        {
            HttpResponseMessage response = await client.GetAsync("http://XXX/");
            response.EnsureSuccessStatusCode();

            string result = await response.Content.ReadAsStringAsync();
            int serverTime = int.Parse(result);
            return serverTime;
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine("Błąd: " + ex.Message);
            throw;
        }
    }
}

async void Test()
{
    Console.WriteLine(await GetServerTime());
}

Test();

When i dont use Test method instead just using
Console.WriteLine(await GetServerTime());

it works normally
Was this page helpful?