How to do it: Consume an WCF 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 XML of the request must be built according the WSDL description of the web service and then encapsulated within the XML of a SOAP message.

Consequently, the XML of the response is formatted as a SOAP message:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <getCategoryWithDataContractAsXMLResponse xmlns="http://www.training.com">
      <getCategoryWithDataContractAsXMLResult xmlns:a="http://schemas.datacontract.org/2004/07/WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Description>test category #10</a:Description>
        <a:Id>1</a:Id>
        <a:Name>category #10</a:Name>
      </getCategoryWithDataContractAsXMLResult>
    </getCategoryWithDataContractAsXMLResponse>
  </s:Body>
</s:Envelope>
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
            {
                string soap =
                        @"<?xml version=""1.0"" encoding=""utf-8""?>
                        <soap:Envelope  xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
                                        xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
                                        xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                          <soap:Body>
                            <getCategoryWithDataContractAsXML xmlns=""http://www.training.com"">
                                <pId>10</pId>
                            </getCategoryWithDataContractAsXML>
                          </soap:Body>
                        </soap:Envelope>";

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

                request.Headers.Add("SOAPAction", @"http://www.training.com/IwcfTestService/getCategoryWithDataContractAsXML");
                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);
                }

                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);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }
    }
}