直接从C#发送原始SOAP XML来WCF服务 [英] Sending raw SOAP XML directly to WCF service from C#

查看:124
本文介绍了直接从C#发送原始SOAP XML来WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务引用:

I have a WCF service reference:

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

和我有一个包含一个兼容的SOAP信封

and I have an XML file containing a compliant SOAP envelope

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <MyXML>
       ...

现在,我想直接发送这些原始数据到服务通过一些C#代码(并接收响应)不使用Visual Studio的服务引用。

Now, I would like to send this raw data directly to the service (and receive the response) via some C# code without using a Visual Studio service reference.

这是可能的,如果是这样,怎么样?

Is this possible, and if so, how?

推荐答案

您可以使用 UploadString 。您需要设置内容类型的SOAPAction 头恰当:

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: <soap:operation soapAction="..." /> 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);
        }
    }
}

这篇关于直接从C#发送原始SOAP XML来WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆