【问题标题】:How to see SOAP message as web service method calling?如何将 SOAP 消息视为 Web 服务方法调用?
【发布时间】:2013-12-18 06:54:20
【问题描述】:

我想查看我发送到 Web 服务的内容作为调用 .Net 中的方法。例如:

var list = service.SomeWebMethd(req);

我想看看我作为 SOAP 消息发送到 Web 服务的内容。我该怎么办?

【问题讨论】:

    标签: .net web-services soap


    【解决方案1】:

    经过大量搜索和提问后,我设法专门为 C# 编写了这个类,它可以抓取 SOAP 请求和响应信封。希望这对您也有帮助。

    首先创建一个新类并复制粘贴此代码,就像更改命名空间一样。

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    
    //Set namespace to the project name
    namespace yourProjectName // <-- EDIT
    {
        class SOAPRequestResponse : IEndpointBehavior
        {
            public string lastRequestXML
            {
                get
                {
                    return soapInspector.lastRequestXML;
                }
            }
    
            public string lastResponseXML
            {
                get
                {
                    return soapInspector.lastResponseXML;
                }
            }
    
            private MyMessageInspector soapInspector = new MyMessageInspector();
    
            public void AddBindingParameters(ServiceEndpoint endPoint, BindingParameterCollection bindingParameters)
            {
    
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endPoint, EndpointDispatcher endPointDispatcher)
            {
    
            }
    
            public void Validate(ServiceEndpoint endPoint)
            {
    
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endPoint, ClientRuntime clientRuntime)
            {
                clientRuntime.MessageInspectors.Add(soapInspector);
            }
    
            public class MyMessageInspector : IClientMessageInspector
            {
                public string lastRequestXML { get; private set; }
                public string lastResponseXML { get; private set; }
    
                public void AfterReceiveReply(ref Message reply, object corActionState)
                {
                    lastResponseXML = reply.ToString();
                }
    
                public object BeforeSendRequest(ref Message request, IClientChannel channel)
                {
                    lastRequestXML = request.ToString();
                    return request;
                }
            }
        }
    }
    

    其次,您需要在 表单中创建 SOAPRequestRespone 类的新实例。

    SOAPRequestResponse soapENV = new SOAPRequestResponse();
    

    然后您必须将它添加到代理类中(也可以在主窗体中):

    service.Endpoint.Behaviors.Add(soapENV);
    

    最后,您可以将请求和响应信封分配给字符串变量,如下所示:

    string request = soapENV.lastRequestXML;
    string response = soapENV.lastResponseXML;
    

    希望这对您也有帮助。您还可以使用其他工具,例如 SOAPUI。

    【讨论】:

    • 我只使用wireshark或任何其他嗅探器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多