如何拦截 SOAP 消息

发布于 2024-12-27 13:48:00 字数 181 浏览 0 评论 0原文

作为我涉足 WCF 的一部分,我正在研究消息契约并了解它们如何影响 SOAP 消息的内容。

如果您能够拦截该消息并查看其结构,那就太酷了。请问我该怎么做呢...

(到目前为止,我已经研究过 Wireshark(太“低级”)并考虑过 Microsoft SOAP 工具包,但该工具包早在 2005 年就被微软淘汰了)

As part of my venture into WCF I'm looking at message contracts and seeing how they affect the content of the SOAP message.

What would be really cool is if you could intercept the message and see how it is structured. How would I go about doing this please...

(So far I've looked at Wireshark (too 'low-level') and thought about Microsoft SOAP toolkit but this was retired by microsoft back in 2005)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2025-01-03 13:48:00

当您安装 .NET 3.5 或更高版本时,您应该拥有 WCF 测试客户端 在你机器上的某个地方(隐藏在 C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ 等目录深处)。

这个工具允许您连接到您的 WCF 服务,您可以调用它的方法 - 并且您可以查看 XML 请求和响应的所有优点 :-)

在此处输入图像描述

另一种选择是使用免费版本的 SoapUI 这是设计的测试 SOAP 服务并以 XML 形式显示请求和响应

在此处输入图像描述

SoapUI 是一个很棒的工具 - 但它不是特定于 WCF 的,它只是一个“通用”SOAP/WSDL 工具,适用于任何 SOAP 服务。

如果您不是在寻找“按需”捕获请求和响应,但如果您对跟踪所有请求和响应更感兴趣,则应该研究 WCF 跟踪功能 并根据需要进行设置。您可以将所有流量捕获到磁盘上的 *.svclog 文件中,其中有 WCF 服务跟踪查看器(WCF 也免费)用于检查这些跟踪文件。

When you installed .NET 3.5 or up, you should have the WCF Test Client on your machine somewhere (hidden deep inside a directory like C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ or something like that).

This tool allows you to connect to your WCF service, and you can call methods on it - and you can look at the XML request and response in all its beauty :-)

enter image description here

The other option would be to use something like the free version of SoapUI which is designed to test SOAP services and show request and response in XML

enter image description here

SoapUI is a great tool - but it's not WCF specific, it's just a "generic" SOAP/WSDL tool which works great against any SOAP service.

If you're not looking for "on-demand" capturing of requests and responses, but if you're more interested in having a trace of all requests and responses, you should investigate the WCF tracing features and setup them up as needed. You can capture all traffic into a *.svclog file on disk, and there's the WCF Service Trace Viewer (also free with WCF) to inspect those trace files.

葬シ愛 2025-01-03 13:48:00

我通常使用 Fiddler 来检查通过 http 发送的肥皂消息。

I usually use Fiddler to inspect soap messages sent over http.

那请放手 2025-01-03 13:48:00

如果你想写一条日志记录,可以尝试使用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
    }
}

If you want to write a log record is try using TraceExtension class, below an example,
in this link you will find details about how to implement it, I use it and it worked very well

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文