从 C# 将原始 SOAP XML 直接发送到 WCF 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1728293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending raw SOAP XML directly to WCF service from C#
提问by lox
I have a WCF service reference:
我有一个 WCF 服务参考:
http://.../Service.svc(?WSDL)
and I have an XML file containing a compliant SOAP envelope
我有一个包含兼容 SOAP 信封的 XML 文件
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<MyXML>
...
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.
现在,我想通过一些 C# 代码直接将此原始数据发送到服务(并接收响应),而不使用 Visual Studio 服务引用。
Is this possible, and if so, how?
这是可能的,如果是,如何?
采纳答案by Darin Dimitrov
You could use UploadString. You need to set the Content-Type
and SOAPAction
headers appropriately:
您可以使用UploadString。您需要适当地设置Content-Type
和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);
}
}
}
回答by Shiraz Bhaiji
You could try using the webclient class and posting your xml to the service.
您可以尝试使用 webclient 类并将您的 xml 发布到服务。
回答by user984672
I just want to comment that Darin's response worked for me, except that I had to take out the extra quotes around the SOAPAction header value (substitute your uri, of course):
我只想评论 Darin 的响应对我有用,除了我必须去掉 SOAPAction 标头值周围的额外引号(当然替换你的 uri):
client.Headers.Add("SOAPAction", "http://www.example.com/services/ISomeOperationContract/GetContract");