2013-12-18 82 views

回答

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一樣使用。

+0

我只是使用wireshark或任何其他嗅探器。 – jlvaquero

相關問題