Search code examples
web-servicessoapwindows-8wsdlwindows-runtime

Customize SOAP request send from Windows 8 application


I am consuming a WSDL in a Windows 8 app. I need to customize the SOAP request like

New SOAP Request:

</ns0:Header>
<ns0:Body>
<ns0:Request xmlns:ns0="http://www.ABC.co.il/2004/01/RetrieveEntityDetails/EntityDetailsRequest">

</ns0:Request></ns0:Body></ns0:Envelope> 

Current SOAP Request:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>

</s:Header>
<s:Body>
    <RetrieveEntityDetailsXPOP_XmlRequest xmlns="http://tempuri.org/">
<RetrieveEntityDetailsXPOP_Xml xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<requestDoc>
</requestDoc>
</RetrieveEntityDetailsXPOP_Xml>
</RetrieveEntityDetailsXPOP_XmlRequest>
</s:Body>
</s:Envelope>

How will I change the namespace and set a request tag inside the body tag.

Code:

Client = new EAI_RetrieveEntityDetailsXP_ServiceSoapClient();
            Client.RetrieveEntityDetailsXPOP_XmlCompleted += Client_RetrieveEntityDetailsXPOP_XmlCompleted;
            XElement requestData = GetRequestData();

            using (new OperationContextScope(Client.InnerChannel))
            {
                // Create a custom soap header
                var msgHeader = MessageHeader.CreateHeader("myCustomHeader", string.Empty, "myValue");

                // Add the header into request message
                OperationContext.Current.OutgoingMessageHeaders.Add(msgHeader);

                Client.RetrieveEntityDetailsXPOP_XmlAsync(requestData);
            }

Solution

  • I have actually used HttpCleint to call this service by using following code. Using this we can generate the SOAP request as desired.

    string soapString = ConstructSoapRequest();
    
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("SOAPAction", SOAPActionUri);
                    var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
                    using (var response = await client.PostAsync(Uri, content))
                    {
                        var soapResponse = await response.Content.ReadAsStringAsync();
                        return soapResponse;
                    }
                }
    
    
    private string ConstructSoapRequest()
            {
                return String.Format(@"<ns0:Envelope xmlns:ns0='http://www.cellcom.co.il/2004/01/std-headers'>
    <ns0:Header>
    
    </ns0:Header>
    <ns0:Body>
    <ns0:Request xmlns:ns0='http://www.cellcom.co.il/2004/01/RetrieveEntityDetails/EntityDetailsRequest'>
    
    
    </ns0:Request></ns0:Body></ns0:Envelope>", 100);
            }