How do you call an API from C#?

  • To call an API for data, we can use the built-in HttpClient that comes with the System.Net namespace.
using (HttpClient client = new HttpClient())
{
  var response = await client.GetStringAsync("http://referbruv.com");
  Console.WriteLine(response);
}
  • HttpClient implements the IDisposable interface and so can be used under a using block. But it is one of those unmanaged resources which should not be released frequently and instead be reused as much as possible.
  • To call an API for data, we can use the built-in HttpClient that comes with the System.Net namespace.
using (HttpClient client = new HttpClient())
{
  var response = await client.GetStringAsync("https://referbruv.com");
  Console.WriteLine(response);
}
  • HttpClient implements the IDisposable interface and so can be used under a using block. But it is one of those unmanaged resources which should not be released frequently and instead be reused as much as possible.
Default image
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply