Add SOAP Envelope to WCF Service

I have the following SOAP message that I want to send to the WCF service and get a response.

"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\r\n  <s:Header>\r\n    <a:Action s:mustUnderstand=\"1\">http://schemas.devleap.com/OrderService/IOrderService/InsertOrder</a:Action>\r\n    <a:MessageID>urn:uuid:4cb619b7-365b-4108-880f-b302029d03c2</a:MessageID>\r\n    <a:ReplyTo>\r\n      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>\r\n    </a:ReplyTo>\r\n  </s:Header>\r\n  <s:Body>\r\n    <InsertOrder xmlns=\"http://schemas.devleap.com/OrderService\">\r\n      <order xmlns:d4p1=\"http://schemas.devleap.com/OrderService/Order\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n        <d4p1:IdCustomer>2</d4p1:IdCustomer>\r\n        <d4p1:IdOrder>46</d4p1:IdOrder>\r\n        <d4p1:OrderItems xmlns:d5p1=\"http://schemas.devleap.com/OrderService/OrderItems\" xmlns:d5p2=\"http://schemas.devleap.com/OrderService/OrderItem\">\r\n          <d5p1:OrderItem>\r\n            <d5p2:IdProduct>P01</d5p2:IdProduct>\r\n            <d5p2:Quantity>5</d5p2:Quantity>\r\n            <d5p2:EuroPrice>20</d5p2:EuroPrice>\r\n          </d5p1:OrderItem>\r\n          <d5p1:OrderItem>\r\n            <d5p2:IdProduct>P01</d5p2:IdProduct>\r\n            <d5p2:Quantity>5</d5p2:Quantity>\r\n            <d5p2:EuroPrice>20</d5p2:EuroPrice>\r\n          </d5p1:OrderItem>\r\n        </d4p1:OrderItems>\r\n      </order>\r\n    </InsertOrder>\r\n  </s:Body>\r\n</s:Envelope>"

How can I publish this SOAP for WCF service? I tried this, but I get "Remote server returned error: (500) Internal server error." an exception.

        using (var client = new WebClient())
        {

            client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");


            var response = client.UploadString("http://localhost:8000/OrderService", data);

        }

If I remove the title from Envelope and add:

client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
client.Headers.Add("SOAPAction", "\"http://schemas.devleap.com/OrderService/IOrderService/InsertOrder\"");

I get the same error.

If i put

client.Headers.Add("Content-Type", "text/xml; charset=utf-8");

I get an exception that the server is expecting "application / soap + xml; charset = utf-8".

If someone knows how to call it correctly, please help.

Thanks Adrya

+3
source share
1 answer

After more digging, I managed to send SOAP to the service. Here is the code:

        WebClient myWebClient = new WebClient();
        myWebClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
        byte[] byteArray = Encoding.ASCII.GetBytes(postData);
        byte[] responseArray = myWebClient.UploadData("http://localhost:8000/OrderService", "POST", byteArray);
        Console.WriteLine("\nResponse received was {0}", Encoding.ASCII.GetString(responseArray));
        Console.ReadKey();
+1
source

Source: https://habr.com/ru/post/1765229/


All Articles