gpt4 book ai didi

c# - 使用 C# 构造 SOAP 信封 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:17 27 4
gpt4 key购买 nike

我到处搜索,但我不知道在 C# 中构造这样的 XML 的最佳方法是什么。

    <?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns5572:calculate_something xmlns:ns5572="http://tempuri.org">
<Input_data>
<code_user xsi:type="xsd:string">test_user</code_user>
<password_broker xsi:type="xsd:string">test_password</password>
<subuser_id xsi:type="xsd:string"></subuser_id>
</Input_data>
</ns5572:calculate_something>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我的问题是是否有针对这种结构的特殊专用类。提前致谢。

最佳答案

这是用于调用某些 SOAP Web 服务的 XML,要通过 C# 调用它,您可以将其添加为对 C# 项目的服务引用。

您需要指向您服务的 WSDL(Web 服务定义语言文件)的链接,然后您可以将服务引用添加到您的项目并以这种方式轻松调用它的任何功能:

1- 定义一个客户端来调用服务:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();

2- 以这种方式调用此客户端的某些方法:

client.calculate_something("test_user", "test_password", "");

或者这个:

client.calculate_something(new Input_data() 
{ code_user = "test_user", password_broker = "test_password", subuser_id = ""}
);

This article将帮助您将服务引用添加到您的 C# 项目。

要拦截请求和响应的 XML,请实现这两个类:

public class InspectorBehavior : IEndpointBehavior
{
public string LastRequestXML {
get
{
return myMessageInspector.LastRequestXML;
}
}

public string LastResponseXML {
get
{
return myMessageInspector.LastResponseXML;
}
}


private MyMessageInspector myMessageInspector = new MyMessageInspector();
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{

}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{

}

public void Validate(ServiceEndpoint endpoint)
{

}


public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(myMessageInspector );
}
}





public class MyMessageInspector : IClientMessageInspector
{
public string LastRequestXML { get; private set; }
public string LastResponseXML { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
LastResponseXML = reply.ToString();
}

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
LastRequestXML = request.ToString();
return request;
}
}

然后,将调用代码更改为:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();
var requestInterceptor = new InspectorBehavior();
client.Endpoint.Behaviors.Add(requestInterceptor );
client.calculate_something("test_user", "test_password", "");
string requestXML = requestInterceptor.LastRequestXML;
string responseXML = requestInterceptor.LastResponseXML;
// Now the xml you need is in "requestXML" variable

关于c# - 使用 C# 构造 SOAP 信封 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779171/

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