© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
25 replies
fooo1

❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?

I do not get anything past making a request. Neither an exception, nor an exception. I'm guessing it's because I'm not properly waiting for async to complete - but in that case what is it lacking?

Program.cs
HttpClientLib httpClientLib = new();
httpClientLib.RequestGet();
HttpClientLib httpClientLib = new();
httpClientLib.RequestGet();


HttpClientLib.cs
using System.Net.Http;

public class HttpClientLib
{
    public async void RequestGet()
    {
        try
        {
            using (var client = new HttpClient()) {
                var url = "https://dummy.restapiexample.com/api/v1/employees";
                HttpResponseMessage response = await client.GetAsync(url);
                Console.WriteLine(response.StatusCode);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Generic exception caught: {ex.Message}");
        }
    }
}
using System.Net.Http;

public class HttpClientLib
{
    public async void RequestGet()
    {
        try
        {
            using (var client = new HttpClient()) {
                var url = "https://dummy.restapiexample.com/api/v1/employees";
                HttpResponseMessage response = await client.GetAsync(url);
                Console.WriteLine(response.StatusCode);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Generic exception caught: {ex.Message}");
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

why this doesn't work
C#CC# / help
2y ago
✅ why doesn't this work?
C#CC# / help
3y ago
❔ Why doesn't this button work?
C#CC# / help
3y ago
seems like this doesn't work as intended!
C#CC# / help
2y ago