Forum Discussion

KLWallace's avatar
KLWallace
New Contributor
14 days ago
Solved

SOAP Request works in SoapUI, but not in C# code

This envelope successfully contacts an API endpoint and returns data as expected.

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inv="http://invoiceworks.source.trax.aero">
   <soapenv:Header/>
   <soapenv:Body>
      <inv:queryRecords soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <query xsi:type="trax:InvoiceWorks_Query_Element" xmlns:trax="http://trax.invoiceworks.castor.trax.aero">
            <batchSize xsi:type="xsd:int">100</batchSize>
            <getOnlyUnSentRecords xsi:type="xsd:boolean">true</getOnlyUnSentRecords>
            <use_Ri_As_Received xsi:type="xsd:boolean">true</use_Ri_As_Received>
         </query>
      </inv:queryRecords>
   </soapenv:Body>
</soapenv:Envelope>

However, that same envelope in my C# project fails with 500 Internal Server Error.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8080/WebModuleTraxInterface/services/PurchaseOrder");

request.Method = "POST";
request.ContentType = "text/xml; charset=\"utf-8\"";
request.Headers.Add("Accept-Encoding", "gzip,deflate");

XmlDocument xml = new XmlDocument();

xml.LoadXml(
    @"<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
        xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
        xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
        xmlns:inv=""http://invoiceworks.source.trax.aero"">
       <soapenv:Header/>
       <soapenv:Body>
          <inv:queryRecords soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
             <query xsi:type=""trax:InvoiceWorks_Query_Element"" xmlns:trax=""http://trax.invoiceworks.castor.trax.aero"">
                <batchSize xsi:type=""xsd:int"">100</batchSize>
                <getOnlyUnSentRecords xsi:type=""xsd:boolean"">true</getOnlyUnSentRecords>
                <use_Ri_As_Received xsi:type=""xsd:boolean"">true</use_Ri_As_Received>
             </query>
          </inv:queryRecords>
       </soapenv:Body>
    </soapenv:Envelope>"
);

using (Stream stream = request.GetRequestStream())
{

    xml.Save(stream);

    using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
    {
        string response = reader.ReadToEnd();
    }
}

Is there something, or somewhere in SoapUI that sends additional data or headers in the request that I am not sending in my code?  Is there some setting in SoapUI that I am unaware of that I need to also do in my code?

 

KLW

 

  • Turns out the SoapUI interface had a configured "SOAP Action", that I was not adding as a header in my HTTPWebRequest.  Problem solved by adding:

    request.Headers.Add("SOAPAction", "http://contoso.com/queryRecordsRequest");

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    @KLWallace

    When you send the request from SoapUI, open the raw tab of the request editor where all the request headers will be shown.

  • malziotti's avatar
    malziotti
    Occasional Contributor

    You can try reading the backend logs and understand what it writes when it receives the request from C# client. Maybe you will find some information.

  • KLWallace's avatar
    KLWallace
    New Contributor

    Turns out the SoapUI interface had a configured "SOAP Action", that I was not adding as a header in my HTTPWebRequest.  Problem solved by adding:

    request.Headers.Add("SOAPAction", "http://contoso.com/queryRecordsRequest");