c# – 有没有办法从ASP.NET WebMethod中获取原始的SOAP请求?

前端之家收集整理的这篇文章主要介绍了c# – 有没有办法从ASP.NET WebMethod中获取原始的SOAP请求?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例:
public class Service1 : System.Web.Services.WebService
{
   [WebMethod]
   public int Add(int x,int y)
   {
       string request = getRawSOAPRequest();//How could you implement this part?
       //.. do something with complete soap request

       int sum = x + y;
       return sum;
   }
}

解决方法

SoapExtensions的替代方法是实现IHttpModule,并在输入流中获取输入流.
public class LogModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += this.OnBegin;
    }

    private void OnBegin(object sender,EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;

        byte[] buffer = new byte[context.Request.InputStream.Length];
        context.Request.InputStream.Read(buffer,buffer.Length);
        context.Request.InputStream.Position = 0;

        string soapMessage = Encoding.ASCII.GetString(buffer);

        // Do something with soapMessage
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}
原文链接:https://www.f2er.com/csharp/94312.html

猜你在找的C#相关文章