这是用于调用一些 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 = ""}
);
本文将帮助您将服务引用添加到您的 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