Hat Tip 1 3 – Http Web Services Client Connect

broken image
Connect

The .NET 2.0 included WebClient class to communicate with web server using HTTP protocol. However, WebClient class had some limitations. The .NET 4.5 includes HttpClient class to overcome the limitation of WebClient. Here, we will use HttpClient class in console application to send data to and receive data from Web API which is hosted on local IIS web server. You may use HttpClient in other .NET applications also such as MVC Web Application, windows form application, windows service application etc.

Let's see how to consume Web API using HttpClient in the console application.

We will consume the following Web API created in the previous section.

Step 1:

First, create a console application in Visual Studio 2013 for Desktop.

  1. Amazon Web Services - Migrating Your Existing Applications to the AWS Cloud October 2010 Page 3 of 23 Introduction Developers and architects looking to build new applications in the cloud can simply design the components, processes and workflow for their solution, employ the APIs of the cloud of their choice, and leverage the latest cloud-based best.
  2. I am trying to find the nearest store given a zip code. I came to know that yelp and foursquare provides the required APIs to do this. I am using.NET 3.5 framework. How do you make the http requests and handle the responses.Most of the solns on the web give it for.NET 4.5 onwards which includes the usage of HTTPClient class.

Step 2:

WSDL (Web services description language) A web service cannot be used if it cannot be found. The client invoking the web service should know where the web service actually resides. Secondly, the client application needs to know what the web service actually does, so that it can invoke the right web service.

Open NuGet Package Manager console from TOOLS -> NuGet Package Manager -> Package Manager Console and execute following command.

Distance 1 0. Install-Package Microsoft.AspNet.WebApi.Client

Step 3:

Now, create a Student model class because we will send and receive Student object to our Web API.

Send GET Request

The following example sends an HTTP GET request to Student Web API and displays the result in the console.

Let's understand the above example step by step.

First, we have created an object of HttpClient and assigned the base address of our Web API. The GetAsync() method sends an http GET request to the specified url. The GetAsync() method is asynchronous and returns a Task. Task.wait() suspends the execution until GetAsync() method completes the execution and returns a result.

Once the execution completes, we get the result from Task using Task.result which is HttpResponseMessage. Now, you can check the status of an http response using IsSuccessStatusCode. Read the content of the result using ReadAsAsync() method.

Thus, you can send http GET request using HttpClient object and process the result.

Send POST Request

Similarly, you can send HTTP POST request using PostAsAsync() method of HttpClient and process the result the same way as GET request.

The following example send http POST request to our Web API. It posts Student object as json and gets the response.

The following table lists all the methods of HttpClient to send different HTTP requests.

Method Name Description
GetAsync Sends a GET request to the specified Uri as an asynchronous operation.
GetByteArrayAsync Sends a GET request to the specified Uri and returns the response body as a byte array in an asynchronous operation.
GetStreamAsync Sends a GET request to the specified Uri and returns the response body as a stream in an asynchronous operation.
GetStringAsync Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation.
PostAsync Sends a POST request to the specified Uri as an asynchronous operation.
PostAsJsonAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as JSON.
PostAsXmlAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as XML.
PutAsync Sends a PUT request to the specified Uri as an asynchronous operation.
PutAsJsonAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value serialized as JSON.
PutAsXmlAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value serialized as XML.
DeleteAsync Sends a DELETE request to the specified Uri as an asynchronous operation.

Visit MSDN to know all the members of HttpClient and HttpClientExtension.

The .NET 2.0 included WebClient class to communicate with web server using HTTP protocol. However, WebClient class had some limitations. The .NET 4.5 includes HttpClient class to overcome the limitation of WebClient. Here, we will use HttpClient class in console application to send data to and receive data from Web API which is hosted on local IIS web server. You may use HttpClient in other .NET applications also such as MVC Web Application, windows form application, windows service application etc.

Let's see how to consume Web API using HttpClient in the console application.

We will consume the following Web API created in the previous section.

Step 1:

First, create a console application in Visual Studio 2013 for Desktop.

Step 2:

Open NuGet Package Manager console from TOOLS -> NuGet Package Manager -> Package Manager Console and execute following command.

Install-Package Microsoft.AspNet.WebApi.Client

Step 3:

Now, create a Student model class because we will send and receive Student object to our Web API.

Send GET Request

The following example sends an HTTP GET request to Student Web API and displays the result in the console.

Let's understand the above example step by step.

First, we have created an object of HttpClient and assigned the base address of our Web API. The GetAsync() method sends an http GET request to the specified url. The GetAsync() method is asynchronous and returns a Task. Task.wait() suspends the execution until GetAsync() method completes the execution and returns a result.

Once the execution completes, we get the result from Task using Task.result which is HttpResponseMessage. Now, you can check the status of an http response using IsSuccessStatusCode. Read the content of the result using ReadAsAsync() method.

Thus, you can send http GET request using HttpClient object and process the result.

Send POST Request

Similarly, you can send HTTP POST request using PostAsAsync() method of HttpClient and process the result the same way as GET request.

The following example send http POST request to our Web API. It posts Student object as json and gets the response.

The following table lists all the methods of HttpClient to send different HTTP requests.

Method Name Description
GetAsync Sends a GET request to the specified Uri as an asynchronous operation.
GetByteArrayAsync Sends a GET request to the specified Uri and returns the response body as a byte array in an asynchronous operation.
GetStreamAsync Sends a GET request to the specified Uri and returns the response body as a stream in an asynchronous operation.
GetStringAsync Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation.
PostAsync Sends a POST request to the specified Uri as an asynchronous operation.
PostAsJsonAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as JSON.
PostAsXmlAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as XML.
PutAsync Sends a PUT request to the specified Uri as an asynchronous operation.
PutAsJsonAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value serialized as JSON.
PutAsXmlAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value serialized as XML.
DeleteAsync Sends a DELETE request to the specified Uri as an asynchronous operation.

Hat Tip 1 3 – Http Web Services Client Connector

Visit MSDN to know all the members of HttpClient and HttpClientExtension.





broken image