How to do it: Consume an WCF ReST Service through an HTTP web request

Code Samples
Table of Contents
Summary
Client application invoking WCF service through web request

Client application invoking WCF service through web request

Description

The WCF ReSt service will GET or POST a plan XML/JSON text message. Consequently, the XML /JSON of the response is also XML/JSON text that can be consumed directly.

Operations
File "Program.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Net;

namespace WCF
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                // *** GET category 

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:35799/training/wcf/app/testservice1/REST/Category/10/xml");

                request.Accept = "text/xml";
                request.Method = "GET";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string responseText = string.Empty;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sreader = new StreamReader(responseStream))
                    {
                        responseText = sreader.ReadToEnd();
                    }
                }

                Console.WriteLine(responseText);

                // POST category

                string xml =
                        @"<Category xmlns=""http://schemas.datacontract.org/2004/07/WCF"">
                            <Description>Nova categoria de teste</Description>
                            <Id>0</Id>
                            <Name>Categoria de teste</Name>
                          </Category>";

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(xml);
                request = (HttpWebRequest)WebRequest.Create("http://localhost:35799/training/wcf/app/testservice1/REST/Category");

                request.Accept = "text/xml";
                request.Method = "POST";
                request.ContentLength = buffer.Length;
                request.ContentType = "text/xml;charset=\"utf-8\"";                              

                using (Stream requeststream = request.GetRequestStream())
                {
                    requeststream.Write(buffer, 0, (int)request.ContentLength);
                }

                response = (HttpWebResponse)request.GetResponse();

                responseText = string.Empty;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sreader = new StreamReader(responseStream))
                    {
                        responseText = sreader.ReadToEnd();
                    }
                }

                Console.WriteLine(responseText);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }
    }
}