Sending raw SOAP XML directly to WCF service from C#

后端 未结 3 689
陌清茗
陌清茗 2021-01-30 06:09

I have a WCF service reference:

http://.../Service.svc(?WSDL)

and I have an XML file containing a compliant SOAP envelope



        
3条回答
  •  灰色年华
    2021-01-30 06:28

    You could use UploadString. You need to set the Content-Type and SOAPAction headers appropriately:

    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new WebClient())
            {
                // read the raw SOAP request message from a file
                var data = File.ReadAllText("request.xml");
                // the Content-Type needs to be set to XML
                client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
                // The SOAPAction header indicates which method you would like to invoke
                // and could be seen in the WSDL:  element
                client.Headers.Add("SOAPAction", "\"http://www.example.com/services/ISomeOperationContract/GetContract\"");
                var response = client.UploadString("http://example.com/service.svc", data);
                Console.WriteLine(response);
            }
        }
    }
    

提交回复
热议问题