In the post Design a RESTful service with .NET framework 4.0 i've shown how to create a RESTful service using .Net Framework 4.0.

Let's see now how to consume the service through a .Net client application. We will use the WebClient class defined in the System.Net namespace for sending and receiving data to or from the RESTful service, and the DataContractJsonSerializer class to serialize and deserialize an instance of a type to or from a JSON document.

The sample assume that the service is deployed at the address http://localhost:49193/Contacts.svc:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // add a contact
            Contact contact = new Contact() {
               fName = "John",
               lName = "Smith",
               eMail = "j.smith@xxx.com"
            };
            
            contact = Add(contact);

            // update a contact
            contact.eMail = "j.smith@tempuri.com";
            Update(contact);

            // get a single contact
            string id = contact.ContactId.ToString();
            contact = GetContact(id);

            // get all contacts
            List<Contact> contactList = GetAll();

            // delete a contact
            Delete(id);
        }

        /// <summary>
        /// Get all contacts
        /// </summary>
        /// <returns></returns>
        static List<Contact> GetAll()
        {
            // web client
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";

            // invoke the REST method
            byte[] data = client.DownloadData(
                   "http://localhost:49193/Contacts.svc/GetAll");

            // put the downloaded data in a memory stream
            MemoryStream ms = new MemoryStream();
            ms = new MemoryStream(data);

            // deserialize from json
            DataContractJsonSerializer ser =
                   new DataContractJsonSerializer(typeof(List<Contact>));
               
            List<Contact> result = ser.ReadObject(ms) as List<Contact>;

            return result;
        }

        /// <summary>
        /// Get a single contact
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static Contact GetContact(string id)
        {
            // web client
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";

            // invoke the REST method
            byte[] data = client.DownloadData(
                   "http://localhost:49193/Contacts.svc/GetContact/" + id);

            // put the downloaded data in a memory stream
            MemoryStream ms = new MemoryStream();
            ms = new MemoryStream(data);

            // deserialize from json
            DataContractJsonSerializer ser =
                   new DataContractJsonSerializer(typeof(Contact));          
            Contact result = ser.ReadObject(ms) as Contact;

            return result;
        }

        /// <summary>
        /// Add a contact
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        static Contact Add(Contact c)
        {
            // web client
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";

            // serialize the object data in json format
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser =
                   new DataContractJsonSerializer(typeof(Contact));
            ser.WriteObject(ms, c);

            // invoke the REST method
            byte[] data = client.UploadData(
                   "http://localhost:49193/Contacts.svc/Add",
                   "ADD",
                   ms.ToArray());

            // deserialize the data returned by the service
            ms = new MemoryStream(data);
            Contact result = ser.ReadObject(ms) as Contact;

            return result;
        }

        /// <summary>
        /// Update the contact
        /// </summary>
        /// <param name="c"></param>
        static void Update(Contact c)
        {
            // web client
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";

            // serialize the object data in json format
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser =
                   new DataContractJsonSerializer(typeof(Contact));
            ser.WriteObject(ms, c);

            // invoke the REST method
            client.UploadData(
                    "http://localhost:49193/Contacts.svc/Update",
                    "PUT",
                    ms.ToArray());
        }

        /// <summary>
        /// Delete the contact
        /// </summary>
        /// <param name="id"></param>
        static void Delete(string id)
        {
            // web client
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";

            // serialize the object data in json format
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser =
                   new DataContractJsonSerializer(typeof(string));
            ser.WriteObject(ms, id);

            // invoke the REST method
            byte[] data = client.UploadData(
                   "http://localhost:49193/Contacts.svc/Delete",
                   "DELETE",
                   ms.ToArray());
        
        }
    }

    public class Contact
    {
        public int ContactId { get; set; }
        public string fName { get; set; }
        public string lName { get; set; }
        public string eMail { get; set; }
    }

}