Consuming JSON REST or RESTful Web Service's Response using .NET / C# Client

Representational State Transfer (REST) is not SOAP based Service and it exposing a public API over the internet to handle CRUD operations on data. REST is focused on accessing named resources through a single consistent interface (URL). REST uses these operations and other existing features of the HTTP protocol:

  1. GET
  2. POST
  3. PUT
  4. DELETE

From developer's points of view, it is not as simple as handling the Object based Module which is supported when we create SOAP url as reference or create WSDL proxy.

But .NET does have Class to deal with JSON restful service.

This document will only cover "how to deal JSON response as a Serialized Object for READ/WRITE & convert JSON object into meanful Object".

In order to consumer JSON Restful service , we need to do follow steps.

  1. Create the RestfUL request URI.
  2. Post URI and get the response from “HttpWebResponse” .
  3. Convert ResponseStreem into Serialized object from “DataContractJsonSerialized” function.
  4. Get the particular results/items from Serialized Object.

This is very generic function which can be used for any rest service to post and get the response and convert in:

  1. public static object MakeRequest(string requestUrl, object JSONRequest, string JSONmethod, string JSONContentType, Type JSONResponseType) {  
  2.   
  3.     try {  
  4.         HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;  
  5.         //WebRequest WR = WebRequest.Create(requestUrl);   
  6.         string sb = JsonConvert.SerializeObject(JSONRequest);  
  7.         request.Method = JSONmethod;  
  8.         // "POST";request.ContentType = JSONContentType; // "application/json";   
  9.         Byte[] bt = Encoding.UTF8.GetBytes(sb);  
  10.         Stream st = request.GetRequestStream();  
  11.         st.Write(bt, 0, bt.Length);  
  12.         st.Close();  
  13.   
  14.   
  15.         using(HttpWebResponse response = request.GetResponse() as HttpWebResponse) {  
  16.   
  17.             if (response.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format(  
  18.                 "Server error (HTTP {0}: {1}).", response.StatusCode,  
  19.             response.StatusDescription));  
  20.   
  21.             // DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));// object objResponse = JsonConvert.DeserializeObject();Stream stream1 = response.GetResponseStream();   
  22.             StreamReader sr = new StreamReader(stream1);  
  23.             string strsb = sr.ReadToEnd();  
  24.             object objResponse = JsonConvert.DeserializeObject(strsb, JSONResponseType);  
  25.   
  26.             return objResponse;  
  27.         }  
  28.     } catch (Exception e) {  
  29.   
  30.         Console.WriteLine(e.Message);  
  31.         return null;  
  32.     }  
  33. }  
to object.

Ebook Download
View all
Learn
View all