将SOAP Envelope发布到WCF服务

时间:2010-09-17 06:18:44

标签: wcf soap

我有以下SOAP消息,我想将其发布到我的WCF服务并获得响应。

"<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>"

如何将此SOAP发布到WCF服务? 我试过这个,但我得到“远程服务器返回错误:(500)内部服务器错误。”异常。

        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);

        }

如果我从信封中删除标题并添加:

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

我得到同样的错误。

如果我把

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

我得到服务器期望“application / soap + xml; charset = utf-8”的异常。

如果有人知道怎么称呼它,请帮助。

谢谢你, Adrya

1 个答案:

答案 0 :(得分:1)

经过更多挖掘后,我设法将SOAP发布到服务中。 这是代码:

        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();