Calling A Web Service From C# With The HttpClient

Webservice Oct 12, 2020

In this tutorial you will learn how to call any web service - like a RESTful - with the in-built HttpClient in dot net core applications.

Creating a test application

For the sake of this tutorial I will create a test console application, but you probably already have an application where you want to add this functionality. If so, just skip this step, the rest should follow along. For creating the test application make sure you have the dot net core command line interface (CLI) installed.

First of all I create a folder demowebservice in my dev folder via the terminal and then jump into it.

patrick@MacBook-Pro dev % mkdir callwebservice
patrick@MacBook-Pro dev % cd callwebservice 

In the next step we create a console application via the CLI in-built templates. Your terminal output should look similar to mine.

patrick@MacBook-Pro callwebservice % dotnet new console
The template "Console Application" was created successfully.

Now let's jump into Visual Studio Code, but no worries, you can choose whatever editor you want.

Just by typing in code . the current folder gets opened with Visual Studio Code.

patrick@MacBook-Pro callwebservice % code .

To verify that everything is installed correctly run the created console application ones. The default output to the console should be "Hello World!".

patrick@MacBook-Pro callwebservice % dotnet run
Hello World!

Calling The Web Service Endpoint

The next step is to call the web service endpoint. For this tutorial, I am going to use the web service from my last blog post. The web service returns data at the WeatherForecast endpoint.

For the simplicity of this tutorial we create a basic GetData() method.

static async Task<string> GetData()
{
    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync("http://localhost:5000/WeatherForecast");
    var textContent = await response.Content.ReadAsStringAsync();

    httpClient.Dispose();

    return textContent;
}

This method creates a new instance of the HttpClient. In the next line we call the specified http://localhost:5000/WeatherForecast endpoint from my last blog post. You can use any other web service endpoint you can find. Even https://google.com should work fine.

Now, because we are using a console application we have one limitation. Our Main method can't be async, so we have to resolve the Task before we can continue. Try to avoid using GetAwaiter() and GetResult() in any production environment. It is counter-productive to the async-await thinking.

static void Main(string[] args)
{
    string data = GetData().GetAwaiter().GetResult();
    Console.Write($"returned data: {data}");
}

The next step is to run the application again and observe the output.

Viola, you should see something like this. If you are seeing any error double-check the request URL in your request.

patrick@MacBook-Pro callwebservice % dotnet run
returned data: [{"date":"2020-10-11T11:50:31.111633+02:00","temperatureC":39,"temperatureF":102,"summary":"Warm"},{"date":"2020-10-12T11:50:31.111709+02:00","temperatureC":-18,"temperatureF":0,"summary":"Scorching"},{"date":"2020-10-13T11:50:31.11171+02:00","temperatureC":54,"temperatureF":129,"summary":"Scorching"},{"date":"2020-10-14T11:50:31.11171+02:00","temperatureC":32,"temperatureF":89,"summary":"Hot"},{"date":"2020-10-15T11:50:31.11171+02:00","temperatureC":-6,"temperatureF":22,"summary":"Warm"}]%    

That's it. Now you can call the world with your dot net core applications and the in-built HttpClient. Check out all the other methods like POST, PUT, DELETE and many more directly on the HttpClient object.

Note: When you are using more sophisticated applications use the IHttpClientFactory to create your HttpClient.

Let me know about your experience with web service in the comments below.

Happy hacking folks.

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.