0

I am new to web services and need to capture SOAP XML messages that will be sent to my web service. I found article that says you can read the contents of the Request.InputStream from within your asmx WebMethod.

Capturing SOAP requests to an ASP.NET ASMX web service

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 begining 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;
    }
  }
}

However, I am confused because this asks for an int input parameter. I suppose I could just remove it, but I am not sure how the external user would call my web service and post an XML message to it. How could I perform a test on this to send it XML messages and make sure I can capture them in the stream? Any tips or links would be greatly appreciated, thanks.

Community
  • 1
  • 1
Stan
  • 1
  • 1
  • 2

1 Answers1

0

"I am new to web services and need to capture SOAP XML messages that will be sent to my web service."

SOAP is just a protocol for exchanging information between the consumer of the webservice and the webservice. Consider it a handshake of sorts where the data to be passed to the web service is packaged into a SOAP envelope. The consumer of the webservice will send you the data packed into a SOAP envelope. This begs the question - How will the consumer know what to send?

When you open your webservices page it should show you a list of operations supported. If you click on EchoSoapRequest you will see a sample SOAP request that should be sent to your service and its response. All you need to do is handle the parameters that the user sends in your code.

In this case your webservice expects an int. The user who consumes your web service will send an int packed within the SOAP envelope. If you want your user to send a string then declare a string as the input.

PS: As a side note you should look at RESTful web services and their advantages if you are starting from scratch.

eightyeight
  • 498
  • 3
  • 6
  • Thanks for your response. My requirements are to use SOAP instead of RESTful. I don't think I explained myself correctly. I have a WSDL of the XML that will be sent to me. That means once I capture the XML I know how to use read the values. I don't belive my webservice expects an int or a string, but the XML in the format of the WSDL. Do I have to somehow validate what is sent with the WSDL file? Or do I just capture any XML in the stream? Sorry I am bit confused. – Stan May 11 '13 at 19:33
  • Your WSDL shows you the SOAP call that is required to hit your web service. Now, if your webservice expects XML then you can change your input parameter to XmlElement input instead of int input. That way the consumer can send XML and you can parse it within your code. Take a look at this [link](http://msdn.microsoft.com/en-us/library/aa480498.aspx) – eightyeight May 12 '13 at 12:49