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.

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *