How to do it: Consume an ASP.NET Web Service through an HTTP web request

Code Samples
Table of Contents
Summary

Visual Studio solution-ASP.NET Web Service Client (HttpWebRequest)

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:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <getCategoryResponse xmlns="http://www.training.com/">
      <Category xmlns="http://www.training.com">
        <Id>1</Id>
        <Name>category #10</Name>
        <Description>test category #10</Description>
      </Category>
    </getCategoryResponse>
  </soap:Body>
</soap:Envelope>

In order to be used the SOAP envelope must be removed.

Operations
File "Program.cs", using SOAP 1.1
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 ASPNET
{
    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>
                            <getCategory xmlns=""http://www.training.com/"">
                                <pId>10</pId>
                            </getCategory>
                          </soap:Body>
                        </soap:Envelope>";

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(soap);
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://localhost:63706/TestService.asmx");

                request.Headers.Add("SOAPAction", @"http://www.training.com/getCategory");
                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);
            }
        }
    }
}
File "Program.cs", using SOAP 1.2
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 ASPNET
{
    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://www.w3.org/2003/05/soap-envelope"">
                          <soap:Body>
                            <getCategory xmlns=""http://www.training.com/"">
                                <pId>10</pId>
                            </getCategory>
                          </soap:Body>
                        </soap:Envelope>";

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(soap);
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://localhost:63706/TestService.asmx");

                request.Headers.Add("Action", @"http://www.training.com/getCategory");
                request.Accept = "text/xml";
                request.Method = "POST";
                request.ContentLength = buffer.Length;
                request.ContentType="application/soap+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);
            }
        }
    }
}