使用HttpModule拦截SOAP web服务调用,并进行REST API调用以传递响应



我需要在现有的遗留.asmx web服务中拦截web方法调用,读取请求以识别需要由新的web API端点处理的业务案例。

与asmx服务对话的客户端也是一些遗留系统,可能添加了服务引用并进行web方法调用,而不知道要进行直接调用的新restapi。因此,应该不知道更改,并一如既往地使用SOAP响应。

到目前为止,我已经添加了一个自定义的HttpModule类,拦截请求,读取输入流,并进行api调用以从中获取响应。但是,我如何将SOAP响应发送回客户端并结束请求,使其不会转到目标web方法。

这是我的自定义HttpModule类:

public class CustomHttpModule : IHttpModule
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }
    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }
    private void Application_BeginRequest(Object source, EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        // Initialize soap request XML
        XmlDocument xmlSoapRequest = new XmlDocument();
        // Move to begining of input stream and read
        context.Request.InputStream.Position = 0;
        StreamReader readStream = new StreamReader(context.Request.InputStream, Encoding.UTF8);
        // Load into XML document
        if (readStream.BaseStream != null && readStream.BaseStream.Length > 0)
        {
            //string streamString = readStream.ReadToEnd();
            xmlSoapRequest.Load(readStream);
        }
        context.Request.InputStream.Position = 0;
    // Check if the request is for the web method to be intercepted 
        if (xmlSoapRequest.GetElementsByTagName("SomeWebMethodName").Count > 0)
        {
                string response = ""; 
                using (WebClient wc = new WebClient())
                {
                    string URI = "http://localhost:19875/api/home";
                    wc.Headers[HttpRequestHeader.Accept] = "application/xml";
                    wc.Headers[HttpRequestHeader.ContentType] = "application/xml";
                    response = wc.UploadString(URI, xmlSoapRequest.InnerText);                                                
                }
                context.Response.ContentType = "text/xml";
                context.Response.Write(response);
                context.Response.End();
        }
    }
}

我现在在客户端上收到错误"无法识别的消息版本"。我猜响应应该是SOAP响应,我该如何创建它?此外,是否有更好的方法来解决整个问题?非常感谢。

我已经处理好了。基本上,我必须像一样将输出嵌入SOAP响应主体结构中

                var soapResponse = new StringBuilder("<?xml version="1.0" encoding="utf-8"?>")
                                   .Append("<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">")
                                   .Append("<soap:Body>")
                                   .Append("<WebMethodResponse xmlns="http://tempuri.org/">")
                                   .Append(String.Format("<WebMethodResult>{0}</WebMethodResult>",HttpUtility.HtmlEncode(webApiResponse)))
                                   .Append("</WebMethodResponse>")
                                   .Append("</soap:Body>")
                                   .Append("</soap:Envelope>");
                context.Response.ContentType = "text/xml";
                context.Response.Write(soapResponse);
                context.Response.End();

要像上面那样手动创建SOAP响应,我必须知道当web方法直接为请求提供服务时,响应xml是什么样子的。为此,我们可以插入SOAP扩展来拦截传出的消息流,并读取它,如中所示http://www.codeproject.com/Articles/445007/Intercept-a-raw-SOAP-message

最新更新