title: SOAP server crashes when sending a request
body: <p>I have a SOAP server that I need to request and send commands to.</p>
  <p>First I developed just a listener and printed the data to the console to see if the server is publishing correctly to the right port number and it works here is my code:</p>
  <pre><code>class listener
  {
      public static void Main()
      {
          TcpListener server = null;
          try
          {
              // Set the TcpListener on port 9005.
              Int32 port = 9005;
              IPAddress localAddr = IPAddress.Parse(&quot;0.0.0.0&quot;);
  
              // TcpListener server = new TcpListener(port);
              server = new TcpListener(localAddr, port);
  
              // Start listening for client requests.
              server.Start();
  
              // Buffer for reading data
              Byte[] bytes = new Byte[256];
              String data = null;
  
              // Enter the listening loop.
              while (true)
              {
  
                  Console.Write(&quot;Waiting for a connection... &quot;);
  
                  TcpClient client = server.AcceptTcpClient();
                  Console.WriteLine(&quot;Connected!&quot;);
  
                  data = null;
  
                  // Get a stream object for reading and writing
                  NetworkStream stream = client.GetStream();
  
                  int i;
  
                  // Loop to receive all the data sent by the client.
                  while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                  {
                      // Translate data bytes to a ASCII string.
                      data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                      Console.WriteLine(&quot;Received: {0}&quot;, data);
  
                      // Process the data sent by the client.
                      data = data.ToUpper();
  
                      byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  
                  }
  
                  // Shutdown and end connection
                  client.Close();
              }
          }
          catch (SocketException e)
          {
              Console.WriteLine(&quot;SocketException: {0}&quot;, e);
          }
          finally
          {
              // Stop listening for new clients.
              server.Stop();
          }
  
          Console.WriteLine(&quot;\nHit enter to continue...&quot;);
          Console.Read();
      }
  }
  </code></pre>
  <p>PS:
  1/server write to port number 9005 and listen on port 9003.
  2/ There is no WSDL available.</p>
  <p>Now I have this request sample from the server documentation that I need to test:</p>
  <pre><code>POST /ot HTTP/1.1
  User-Agent: Dispatcher
  Host: localhost:9003
  Content-Length: 226
  Connection: Close
  
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  &lt;SOAP-ENV:Body&gt;
      &lt;startPSF/&gt;
  &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  <p>I implemented this SOAP client class but the server is crashing and the app tabs are closing when sending the request! Here is my code:</p>
  <pre><code>class SOAPClient
  {
      public static void CallWebService()
      {
          var _url = &quot;http://localhost:9003/ot&quot;;
          var _action = &quot;http://localhost:9003/ot?op=startPSF&quot;;
  
          XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
          HttpWebRequest webRequest = CreateWebRequest(_url, _action);
          InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
  
          // begin async call to web request.
          IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
  
          asyncResult.AsyncWaitHandle.WaitOne();
  
          // get the response from the completed web request.
          string soapResult;
  
          using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
          {           
              using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
              {
                  soapResult = rd.ReadToEnd();
              }
              Console.Write(soapResult);
              webResponse.Close();
              
          }
          webRequest.Abort();
      }
  
      private static HttpWebRequest CreateWebRequest(string url, string action)
      {
  
          HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
          webRequest.Headers.Add(&quot;SOAPAction&quot;, action);
          webRequest.Proxy = GlobalProxySelection.GetEmptyWebProxy();
          webRequest.Host = &quot;localhost:9003&quot;;
          //webRequest.ContentType = &quot;text/xml;charset=\&quot;utf-8\&quot;&quot;;
          webRequest.Accept = &quot;text/xml&quot;;
          webRequest.UserAgent = &quot;Dispatcher&quot;;
          webRequest.Method = &quot;POST&quot;;
          webRequest.ContentLength = 226;
          //webRequest.ProtocolVersion = HttpVersion.Version11;
          webRequest.KeepAlive = false;
  
          return webRequest;
      }
  
      private static XmlDocument CreateSoapEnvelope()
      {
          XmlDocument soapEnvelopeDocument = new XmlDocument();
  
          soapEnvelopeDocument.LoadXml(
              @&quot;&lt;?xml version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;UTF-8&quot;&quot;?&gt;
              &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&quot;&gt;
                  &lt;SOAP-ENV:Body&gt;
                      &lt;startPSF/&gt;
                  &lt;/SOAP-ENV:Body&gt;
              &lt;/SOAP-ENV:Envelope&gt;&quot;);
  
          return soapEnvelopeDocument;
      }
  
      private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
      {
          using (Stream stream = webRequest.GetRequestStream())
          {
              soapEnvelopeXml.Save(stream);
          }
      }
  
      static void Main(string[] args)
      {
          CallWebService();
      }
  }
  </code></pre>
  <p>And here is the error:</p>
  <pre><code>System.Net.WebException
    HResult=0x80131620
    Message=An error occurred while sending the request.
    Source=System.Net.Requests
    StackTrace:
     at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     at soapClient.CallWebService() in C:\Users\pfsClient\soapClient.cs:line 39
     at soapClient.Main() in C:\Users\pfsClient\soapClient.cs:line 117
  
  Inner Exception 1:
  HttpRequestException: An error occurred while sending the request.
  
  Inner Exception 2:
  IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
  
  Inner Exception 3:
  SocketException: An existing connection was forcibly closed by the remote host.
  </code></pre>
  <p>I also tried without async calls and same error!</p>
  <p>PS: I can see with netstat that the server is listening on 0.0.0.0:9003 but when I try with this IP Address instead of localhost:9003 I got this error:</p>
  <pre><code>System.Net.WebException
    HResult=0x80070057
    Message=IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. (Parameter 'hostName') (0.0.0.0:9002)
    Source=System.Net.Requests
    StackTrace:
     at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     at soapClient.CallWebService() in C:\Users\pfsClient\soapClient.cs:line 39
     at soapClient.Main() in C:\Users\pfsClient\soapClient.cs:line 117
  
    This exception was originally thrown at this call stack:
      [External Code]
  
  Inner Exception 1:
  HttpRequestException: IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. (Parameter 'hostName') (0.0.0.0:9002)
  
  Inner Exception 2:
  ArgumentException: IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. (Parameter 'hostName')
  </code></pre>
  <p>After adding proxy (GetEmptyWebProxy), httpVersion and content-type:
  Now I am getting a Time-Out error:</p>
  <pre><code>System.Net.WebException
    HResult=0x80131509
    Message=The operation has timed out.
    Source=System.Net.Requests
    StackTrace:
     at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
     at soapClient.CallWebService() in C:\Users\pfsClient\soapClient.cs:line 39
     at soapClient.Main() in C:\Users\pfsClient\soapClient.cs:line 113
  </code></pre>