c# - 从 C# 将原始 SOAP XML 直接发送到 WCF 服务

标签 c# .net wcf soap

我有一个 WCF 服务引用:

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

我有一个包含兼容 SOAP 信封的 XML 文件

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

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

这是否可能,如果可能,如何实现?

最佳答案

你可以使用 UploadString .您需要适本地设置 Content-TypeSOAPAction header :

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# - 从 C# 将原始 SOAP XML 直接发送到 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1728293/

相关文章:

用于解析 JSON 结果的 C# 类

c# - Nuget 上的 System.Reactive/Rx .NET 4.0

.net - 无法加载文件或程序集“System.Data.SQLite,版本=1.0.109.0 - 当我引用 1.0.109.1 时,为什么它会搜索版本 1.0.109.0

c# - 缓存 ASP.NET AJAX 服务 javascript 代理

WCF 剩余错误处理

c# - MongoDB C# 2 驱动程序 - 无法从 BsonType 'String' 反序列化 'Double'

c# - 将授权 token 传递给 Odata 客户端

c# - 如果队列未锁定,多线程的性能与全局队列的长度有关?

c# - StructureMap 添加多个实例不工作

wcf - 使用SSL时私钥存储在哪里?