2010-10-26 40 views
0

REFCreating a SOAP proxy?如何使用httphandler类来更改SOAP命名空间?

如何使用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> 

回答

1

我没有带设法在一个地方这方面的工作还没有:(

的IHttpHandler允许你修改的SOAPAction的头,但SOAP消息的不是身体。

SoapExtension允许您更改肥皂消息的主体,但不能更改标题中的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(); 
    } 

的SoapExtension:

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

仍拼命想获得它所有的n一个地方,但我已经得到它至少工作:/