简体   繁体   English

使用C#构造SOAP Envelope XML

[英]Construct SOAP Envelope XML using C#

I searched all over and i dont know what is the best way to construct an XML like this in C#. 我搜遍了所有,我不知道在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>

My question is if there are any special dedicated classes for this kind of structure. 我的问题是,这种结构是否有任何特殊的专用类。 Thanks in advance. 提前致谢。

This not some random XML. 这不是一些随机的XML。 It looks like a SOAP request. 它看起来像一个SOAP请求。 Look into this . 看看这个

This is the XML for calling some SOAP web service, to call this via C# you can add it as a service reference to your C# project. 这是用于调用某些SOAP Web服务的XML,通过C#调用它可以将其作为服务引用添加到C#项目中。

You need the link to the WSDL (Web service definition language file) of your service, then you can add the service reference to your project and call any of its functionality easily in this way: 您需要指向服务的WSDL(Web服务定义语言文件)的链接,然后您可以将服务引用添加到项目中并以这种方式轻松调用其任何功能:

1- Define a client to use it to call the service: 1-定义客户端以使用它来调用服务:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();

2- Call some method of this client this way: 2-以这种方式调用此客户端的某些方法:

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

or this: 或这个:

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

This article will help you to add the service reference to your C# project. 本文将帮助您将服务引用添加到C#项目中。

To intercept and XMLs of the request and the response, Implement these two classes: 要拦截请求和响应的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;
    }
}

Then, change the call code to: 然后,将呼叫代码更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM