Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C将原始SOAP XML直接发送到WCF服务#_C#_.net_Wcf_Soap - Fatal编程技术网

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

C# 从C将原始SOAP XML直接发送到WCF服务#,c#,.net,wcf,soap,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#代码将这些原始数据直接发送到服务(并接收响应),而不使用Visu

我有一个WCF服务参考:

http://.../Service.svc(?WSDL)
我有一个包含兼容SOAP信封的XML文件

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

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


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

您可以尝试使用webclient类并将xml发布到服务中。

您可以使用。您需要适当地设置
内容类型
SOAPAction
标题:

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);
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
使用(var client=new WebClient())
{
//从文件中读取原始SOAP请求消息
var data=File.ReadAllText(“request.xml”);
//内容类型需要设置为XML
client.Headers.Add(“内容类型”,“text/xml;charset=utf-8”);
//SOAPAction标头指示要调用的方法
//可以在WSDL:element中看到
client.Headers.Add(“SOAPAction”,“\”http://www.example.com/services/ISomeOperationContract/GetContract\"");
var response=client.UploadString(“http://example.com/service.svc“,数据);
控制台写入线(响应);
}
}
}

我只想说明,Darin的回答对我来说是有效的,除了我必须去掉SOAPAction头值周围的额外引号(当然,替换你的uri):


你曾经做到过吗?怎么做?还没有成功,我很害怕你最终放弃了这个想法吗?你想干什么?为什么不能使用Visual Studio服务引用?在某些方面,它是.NET framework的一个基本功能,所以我不明白您为什么不直接使用该功能。此外,您可以使用VS中的wsdl工具创建一个类,然后使用客户机类进行连接。好奇你是否还在以任何方式处理这个问题,因为它是开放的,其他人会阅读它(注意到目前为止的1023个视图)@drachenstern,我不再处理这个问题了。这是一次手动执行WCF消息安全性的尝试。我还要补充一点,它在99.99%的服务上对我有效。尽管W3网站建议在HTTP+SOAP世界中它是可选的,但仍有一些服务需要在前端设置“内容类型”。请注意,“内容类型”可能必须设置为
client.Headers.Add(“内容类型”,“应用程序/SOAP+xml;字符集=utf-8”)
和SOAP请求输入文件可能需要
为将来的访问者提供一个提示:我必须将编码设置为utf8才能正常工作:
client.encoding=encoding.utf8
client.Headers.Add("SOAPAction", "http://www.example.com/services/ISomeOperationContract/GetContract");