2012-01-17 170 views
1

作爲我進入WCF的風險的一部分,我正在查看消息契約並查看它們如何影響SOAP消息的內容。如何攔截SOAP消息

如果你能截取這條信息並看看它的結構如何,那真的很酷。 (到目前爲止,我已經看過Wireshark(太低級)了,想到了微軟的SOAP工具包,但是2005年微軟公司退休了)

回答

4

當您安裝.NET 3.5或更高版本時,您的機器上應該有WCF Test Client(隱藏在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\之類的目錄內的深處)。

這個工具允許你連接到你的WCF服務,你可以調用它的方法 - 你可以看看它的所有美麗的XML請求和響應:-)

enter image description here

的另一種選擇是使用類似的SoapUI免費的版本,旨在測試SOAP服務並顯示請求和響應的XML

enter image description here

SoapUI是一個很好的工具 - 但它不是而是 WCF特有的,它只是一個「通用」的SOAP/WSDL工具,可以很好地對付任何SOAP服務。

如果您不是在尋找「按需」捕獲請求和響應,但如果您對追蹤所有請求和響應更感興趣,則應調查WCF tracing features並根據需要進行設置。您可以將所有流量捕獲到磁盤上的*.svclog文件中,並且還有WCF Service Trace Viewer(也與WCF一起免費)檢查這些跟蹤文件。

+0

謝謝; WCF測試客戶端正是我所期待的 – SkeetJon

+1

Freakin真棒!這是一個非常有用的小工具!謝謝 –

1

如果你想要寫日誌記錄是試圖在這個環節上使用TraceExtension類,下面一個例子, 您還會發現,我用它如何實現它的細節和它的工作非常出色

http://www.systemdeveloper.info/2013/11/trace-soap-requestresponse-xml-with.html

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Services.Protocols; 
using System.IO; 
using System.Xml; 

namespace PruebaServiciosNBC 
{ 
    class TraceExtension : SoapExtension 
    { 
     private Stream oldStream; 
     private Stream newStream; 

     private static XmlDocument xmlRequest; 
     /// <summary> 
     /// Gets the outgoing XML request sent to PayPal 
     /// </summary> 
     public static XmlDocument XmlRequest 
     { 
      get { return xmlRequest; } 
     } 

     private static XmlDocument xmlResponse; 
     /// <summary> 
     /// Gets the incoming XML response sent from PayPal 
     /// </summary> 
     public static XmlDocument XmlResponse 
     { 
      get { return xmlResponse; } 
     } 

     /// <summary> 
     /// Save the Stream representing the SOAP request 
     /// or SOAP response into a local memory buffer. 
     /// </summary> 
     /// <param name="stream"> 
     /// <returns></returns> 
     public override Stream ChainStream(Stream stream) 
     { 
      oldStream = stream; 
      newStream = new MemoryStream(); 
      return newStream; 
     } 

     /// <summary> 
     /// If the SoapMessageStage is such that the SoapRequest or 
     /// SoapResponse is still in the SOAP format to be sent or received, 
     /// save it to the xmlRequest or xmlResponse property. 
     /// </summary> 
     /// <param name="message"> 
     public override void ProcessMessage(SoapMessage message) 
     { 
      switch (message.Stage) 
      { 
       case SoapMessageStage.BeforeSerialize: 
        break; 
       case SoapMessageStage.AfterSerialize: 
        xmlRequest = GetSoapEnvelope(newStream); 
        CopyStream(newStream, oldStream); 
        break; 
       case SoapMessageStage.BeforeDeserialize: 
        CopyStream(oldStream, newStream); 
        xmlResponse = GetSoapEnvelope(newStream); 
        break; 
       case SoapMessageStage.AfterDeserialize: 
        break; 
      } 
     } 

     /// <summary> 
     /// Returns the XML representation of the Soap Envelope in the supplied stream. 
     /// Resets the position of stream to zero. 
     /// </summary> 
     /// <param name="stream"> 
     /// <returns></returns> 
     private XmlDocument GetSoapEnvelope(Stream stream) 
     { 
      XmlDocument xml = new XmlDocument(); 
      stream.Position = 0; 
      StreamReader reader = new StreamReader(stream); 
      xml.LoadXml(reader.ReadToEnd()); 
      stream.Position = 0; 
      return xml; 
     } 

     /// <summary> 
     /// Copies a stream. 
     /// </summary> 
     /// <param name="from"> 
     /// <param name="to"> 
     private void CopyStream(Stream from, Stream to) 
     { 
      TextReader reader = new StreamReader(from); 
      TextWriter writer = new StreamWriter(to); 
      writer.WriteLine(reader.ReadToEnd()); 
      writer.Flush(); 
     } 

     #region NoOp 
     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="methodInfo"> 
     /// <param name="attribute"> 
     /// <returns></returns> 
     public override object GetInitializer(LogicalMethodInfo methodInfo, 
      SoapExtensionAttribute attribute) 
     { 
      return null; 
     } 

     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="WebServiceType"> 
     /// <returns></returns> 
     public override object GetInitializer(Type WebServiceType) 
     { 
      return null; 
     } 

     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="initializer"> 
     public override void Initialize(object initializer) 
     { 
     } 
     #endregion NoOp 
    } 
}