gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:13:42 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com