title: Calling a SOAP service from C# REST API
body: <p>I have a VBA code which calls a SOAP service and receives an XML response.
  I want to write a C# REST API to call this SOAP service in a similar way.</p>
  
  <p>Referring to the articles here I wrote a SOAP request but it does not works and returns the below error. I am able to call this SOAP service from the VBA code and also POST MAN. Can some one please explain what's wrong with my code?</p>
  
  <pre><code> public class CallSOAPAPIController : ApiController
  {
      [HttpGet]
      public string Extract()
      {
          HttpWebRequest request = CreateWebRequest();
          XmlDocument soapEnvelopeXml = new XmlDocument();
  
          soapEnvelopeXml.LoadXml(@"&lt;?xml version=""1.0"" encoding=""utf-8""?&gt;&lt;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/"" xmlns:inc=""http://www.test-xyz.com/""&gt;&lt;soap:Body&gt;Calendar @Year@Month@1&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;");
  
          using (Stream stream = request.GetRequestStream())
          {
              soapEnvelopeXml.Save(stream);
          }
  
          using (WebResponse response = request.GetResponse())
          {
              using (StreamReader rd = new StreamReader(response.GetResponseStream()))
              {
                  soapResult = rd.ReadToEnd();
                  Console.WriteLine(soapResult);
              }
          }
  
          return soapResult;
      }
  
      /// &lt;summary&gt;
      /// Create a soap webrequest to [Url]
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static HttpWebRequest CreateWebRequest()
      {
          HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://test.production-xyz.com/listofemployees.do?getdetails=all&amp;SOAP");
  
          webRequest.Headers.Add(@"SOAP:Action");
          webRequest.ContentType = "text/xml;charset=\"utf-8\"";
          webRequest.Accept = "text/xml";
          webRequest.Method = "POST";
  
          CallSOAPAPIController obj = new CallSOAPAPIController();
  
          string base64Credentials = obj.GetEncodedCredentials();
          webRequest.Headers.Add("Authorization", "Basic " + base64Credentials);
  
          return webRequest;
      }
  
      private string GetEncodedCredentials()
      {
          string m_Username = "XXXXXX";
          string m_Password = "XXXXXX";
  
          string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
          byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
          return Convert.ToBase64String(byteCredentials);
      }
  
  }
  </code></pre>
  
  <p>The error I get : "The remote server returned an error:(401) Unauthorized"</p>
  
  <p><a href="https://i.stack.imgur.com/qhpku.png" rel="nofollow noreferrer">Unauthorized(401)</a></p>