Hat Tip 1 3 – Http Web Services Client 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.
Create a web server to serve public content and connect to the private Amazon RDS DB Instance. AWS Documentation Amazon Relational Database Service (RDS) User Guide Launch an EC2 instance Install an Apache web server with PHP Connect your Apache web server to your DB instance. Web services is a standardized way or medium to propagate communication between the client and server applications on the World Wide Web. This free Web services tutorial for complete beginners will help you learn web service from scratch. I am writing this tip/trick because after a week of research and trial and error, I finally found a solution to enable.NET communication with a Java Web Service. I was recently tasked with writing a.NET Client for a 3 rd party Java web service. I had the following information about the 3 rd party web service. So in short highlights for REST Web Service. REST Web Service is stateless client-server service model; By passing HTTP verb you can perform server side action over standard HTTP protocol (e.g. GET, POST, LIST, DELETE,HEAD) You can pass parameters via URL query string and via HTTP Headers. This document provides instructions and information for quickly developing secure and scalable Jakarta EE applications. You will learn about setting up the development environment, using the Maven repository, and class loading in deployments. The document also has detailed information about: Logging Remote JNDI lookup Clustering in web applications Jakarta Contexts.
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
Hat Tip 1 3 – Http Web Services Client Connector
Step 3:
Now, create a Student model class because we will send and receive Student object to our Web API.
Send GET Request
Hat Tip 1 3 – Http Web Services Client Connectivity
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.
-->For this walkthrough, you will create a new project and a simple console application that accesses Excel Web Services. This walkthrough assumes you are developing in Visual Studio.
Creating the Project
The following project uses Microsoft Visual Studio 2003.
Note
If you are using Microsoft Visual Studio 2005 or Microsoft Visual Studio 2008, the process to create a new project is slightly different depending on which settings you use in the Visual Studio integrated development environment (IDE).
To create a new project
Start Visual Studio.
On the File menu, point to New, and then click Project. The New Project dialog box appears.
In the Project Type pane, select Visual C# Projects.
In the Templates pane, click Console Application.
In the Name box, type SampleApplication.
In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.
Click OK. Your newly created project appears in Solution Explorer.
In Solution Explorer, a file with the default name of Class1.cs has been added to your project.