Introduction
RESTful web service today becomes a norm in every mobile and
web application. RESTful web services can be created by any language. Here I
will explain how to consume a web service from C# application. I have developed
a webservice using Java Springboot and mysql database. Note the web service created consumes and
produces json data. The web application is hosted on a tomcat application
server which is behind https web server.
Client Component
I have developed a C#
application that will consume web service to show the data in the desktop
interface. I have used windows desktop application using Visual Studio 2017
with .Net 4.5 environment. The Desktop application list of bus routes of local
city transport. The sample response from the server seems like the one below.
As show in the above picture the Json response have 3 major
item. One is status, which is of numeric type, other one is message which is of
string type and the last one is it self a Json object. We need to create a http request to the
server and the request should be done using post method as the service works
only using post method. For the particular request we need to pass a Json
object in the body having two parameters as shown in the picture below. Request
parameter will change depending upon the service requested.
There are two parameters required for this service. These
are request parameter which needs to be passed in the request body as a json
object. If you have a web service that needs to send request parameter to
server for processing remember to pass them in the body method as a Json object
if your server application expecting a Json object in the request method.
HTTP Request :
I have created a function to make a http request to the
server. The function takes four parameters as shown in the below picture,
First parameter is requestUrl is string type parameter that
holds the url that needs to be called for the web service. Second parameter
JSONRequest, is a Json object that holds request parameters. This is passed in
the request body. Third parameter is called JSONMethod which is string type.
The JSONmethod can be “GET”, “POST” or “DELETE” depending upon the service type requested. For this
particular service type the method is “POST”. The last parameter is JSONContentType is a string type parameter
that identifies the content type of the request that is passed in the request
header. The function takes returns a JSON object as shown in the above picture.
Most part of the function is self explanatory.
The first line of the function is
mentioned below.
ServicePointManager.ServerCertificateValidationCallback
= delegate { return true; };
This is required as the server uses self signed SSL certificate
and my desktop does not have the certificate. I have added this to ignore SSL certificate while doing http request from
the client application. For more details you can visit this
link.
Since I am using JSON object in both http request and
response, I have used Newtonsoft.json to manage the json object. It has the inbuilt
method to convert objects to josn and vice versa making life simpler for the
developer.
string sb = JsonConvert.SerializeObject(JSONRequest);
In this line the I am using
Newtonsoft.json to converts json object to String while sending request to the
server.
jsonObject =
JsonConvert.DeserializeObject(jsonresponse);
In the above line I am converting the http response from the server while is of string
type to json object. For more
information related to Newtonsoft.Json you can visit their website. For sample application using C# with Newtonsoft
you can check thiss
link.
No comments:
Post a Comment