【问题标题】:how to use httphandler class to change SOAP namespace?如何使用 httphandler 类更改 SOAP 命名空间?
【发布时间】:2010-10-26 04:21:30
【问题描述】:

参考:Creating a SOAP proxy?

如何使用 HttpHandlers 创建代理并更改 SOAP 请求和响应命名空间?

示例...更改此(1 行)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Example1 xmlns="http://www.domain.com">
      <DoSomething>string</DoSomething>
    </Example1>
  </soap:Body>
</soap:Envelope>

至此(1 行)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Example1 xmlns="http://www.mydomain.com">
      <DoSomething>string</DoSomething>
    </Example1>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

    标签: c# asp.net xml web-services soap


    【解决方案1】:

    我还没有设法让它在一个地方工作:(

    IHttpHandler 允许您修改标头中的 SOAPAction,但不能修改 soap 消息的正文。

    SoapExtension 允许您更改soap 消息的正文,但不能更改标头中的SOAPAction。

    根据我的测试,您需要更改两者才能使其正常工作...

    IHTTPHandler:

            public void ProcessRequest(HttpContext context)
        {
            string soapAction = context.Request.Headers["SOAPAction"];
            context.Request.Headers.Set("SOAPAction", soapAction.Replace("OLD_NAMESPACE", "NEW_NAMESPACE"));
    
            WebServiceHandlerFactory factory = new WebServiceHandlerFactory();
            IHttpHandler handler = factory.GetHandler(context, context.Request.HttpMethod, context.Request.FilePath, context.Request.PhysicalPath);
            handler.ProcessRequest(context);
    
            context.Request.Headers.Set("SOAPAction", soapAction);
    
            context.ApplicationInstance.CompleteRequest();
        }
    

    SOAP 扩展:

        public class ReWriteExtension : SoapExtension
    {
        Stream inputStream;
        Stream outputStream;
        bool bPostDeserialize = false;
    
        public override Stream ChainStream(Stream stream)
        {
            if (!bPostDeserialize)
            {
                //We’re deserializing the message, so stream is our input
                //stream
                this.inputStream = stream;
                this.outputStream = new MemoryStream();
                bPostDeserialize = true;
                return outputStream;
            }
    
            else
            {
                //We’re serializing the message, so stream is the
                //destination of our bits
                this.inputStream = new MemoryStream();
                this.outputStream = stream;
                return inputStream;
            }
        }
    
        public override object GetInitializer(Type serviceType)
        {
            return null;
        }
    
        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            return null;
        }
    
        public override void Initialize(object initializer)
        {
        }
    
        public override void ProcessMessage(SoapMessage message)
        {
            switch (message.Stage)
            {
                case SoapMessageStage.BeforeSerialize:
                    break;
                case SoapMessageStage.AfterSerialize:
                    ReWriteOutput(message);
                    break;
                case SoapMessageStage.BeforeDeserialize:
                    ReWriteInput(message);
                    break;
                case SoapMessageStage.AfterDeserialize:
                    break;
                default:
                    throw new Exception("invalid stage");
            }
        }
    
        public void ReWriteInput(SoapMessage message)
        {
            inputStream.Position = 0;
            var sr = new StreamReader(inputStream);
            var xmlMessage = sr.ReadToEnd();
    
            xmlMessage = xmlMessage.Replace("OLD_NAMESPACE", "NEW_NAMESPACE");
    
            outputStream.Position = 0;
            var sw = new StreamWriter(outputStream);
            sw.WriteLine(xmlMessage);
            sw.Flush();
            outputStream.Position = 0;
        }
    
        public void ReWriteOutput(SoapMessage message)
        {
            inputStream.Position = 0;
            var sr = new StreamReader(inputStream);
            var xmlMessage = sr.ReadToEnd();
    
            xmlMessage = xmlMessage.Replace("NEW_NAMESPACE", "OLD_NAMESPACE");
    
            var sw = new StreamWriter(outputStream);
            sw.WriteLine(xmlMessage);
            sw.Flush();
        }
    
        //void Copy(Stream from, Stream to)
        //{
        //    if (from.CanSeek) from.Position = 0;
        //    if (to.CanSeek) to.Position = 0;
        //    TextReader reader = new StreamReader(from);
        //    TextWriter writer = new StreamWriter(to);
        //    writer.WriteLine(reader.ReadToEnd());
        //    writer.Flush();
        //    if (from.CanSeek) from.Position = 0;
        //    if (to.CanSeek) to.Position = 0;
        //}
    }
    

    仍在拼命尝试将所有内容都集中在一个地方,但我至少可以正常工作:/

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多