Capturing SOAP requests to an ASP.NET ASMX web service

前端 未结 4 1643
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 07:03

Consider the requirement to log incoming SOAP requests to an ASP.NET ASMX web service. The task is to capture the raw XML being sent to the web service.

The incomin

4条回答
  •  星月不相逢
    2020-11-28 07:25

    You know that you dont actually need to create a HttpModule right?

    You can also read the contents of the Request.InputStream from within your asmx WebMethod.

    Here is an article I wrote on this approach.

    Code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    namespace SoapRequestEcho
    {
      [WebService(
      Namespace = "http://soap.request.echo.com/",
      Name = "SoapRequestEcho")]
      public class EchoWebService : WebService
      {
    
        [WebMethod(Description = "Echo Soap Request")]
        public XmlDocument EchoSoapRequest(int input)
        {
          // Initialize soap request XML
          XmlDocument xmlSoapRequest = new XmlDocument();
    
          // Get raw request body
          Stream receiveStream = HttpContext.Current.Request.InputStream;
    
          // Move to beginning of input stream and read
          receiveStream.Position = 0;
          using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
          {
            // Load into XML document
            xmlSoapRequest.Load(readStream);
          }
    
          // Return
          return xmlSoapRequest;
        }
      }
    }
    

提交回复
热议问题