我已经得到了我WSDL.EXE创建了一个简单的C#Web服务代理类。我在远程Web服务调用的方法,它是包括了一堆WS-Addressing和我不想(和服务器窒息),WS-Security头的。这里是原始SOAP请求的例子:

<?xml version="1.0" encoding="utf-8"?>
<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" 
  xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action></wsa:Action>
    <wsa:MessageID>urn:uuid:22f12267-b162-4703-a451-2d1c5c5a619b</wsa:MessageID>
    <wsa:To>http://example.com/wstest</wsa:To>
    <wsse:Security>
      <wsu:Timestamp wsu:Id="Timestamp-5c9f0ef0-ab45-421d-a633-4c4fad26d945">
        <wsu:Created>2009-04-15T16:27:25Z</wsu:Created>
        <wsu:Expires>2009-04-15T16:32:25Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

我不关心的WS-Addressing / WS-Security的东西。我还没有做什么,包括它。在.NET WSE 3.0包似乎被默认添加。有什么办法来摆脱这些?我可以看到我的代理对象没有属性让我删除这些章节。我已经试过:

proxyObject.Addressing.Clear();
proxyObject.Security.Clear();

这些导致一个空引用异常当调用我的web服务方法。

我想要的SOAP请求以看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<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">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

在预先感谢

有帮助吗?

解决方案

好,我结束了使用我已经在过去使用的技术。我创建实现类的 SoapFilter 和<强> PolicyAssertion 允许在发送前我修改SOAP请求的原始XML。下面是一个例子:

    public class MyPolicy : SoapFilter
    {
        public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
        {
            // Remove all WS-Addressing and WS-Security header info
            envelope.Header.RemoveAll();

            return SoapFilterResult.Continue;
        }
    }

    public class MyAssertion : PolicyAssertion
    {
        public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
        {
            return new MyPolicy();
        }

        public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
        {
            return null;
        }
    }

然后在你的web服务代理的构造器,你应用策略:

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]       
 [System.Web.Services.WebServiceBindingAttribute(Name="MyBinding", Namespace="http://example.com")]
    public partial class MyWebClient : WebServicesClientProtocol {

        // ... member variables here

        /// <remarks/>
        public MyWebClient()
        {
            this.Url = "http://example.com";           
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }

            // Apply policy here
            Policy policy = new Policy();
            policy.Assertions.Add(new MyAssertion());
            this.SetPolicy(policy); 
        }
  }

其他提示

以除去寻址报头我用下面的代码在我的应用程序中的一个。

  public override void SecureMessage(SoapEnvelope envelope, Security security)
    {
       //remove addressing Header from envelope

        AddressingHeaders objAH = new AddressingHeaders(envelope);

        objAH.RemoveXml(envelope);


        //Add Wahtever security token you want to add.

        security.Tokens.Add(bla-bla-bla);

    }

我使用的 SecureMessage 的功能,因为我想添加安全令牌,但同样的代码可被用的 ProcessMessage的功能

我不知道你的问题可能不是也已经由简单的不使用WSP>解析

基于该页面中的答案

,我添加下面的代码递归从XML除去特定的报头(由标记名)。

Public Overrides Function ProcessMessage(ByVal envelope As SoapEnvelope) As SoapFilterResult
    ' Remove all WS-Addressing and WS-Security header info
    RemoveTag(envelope.DocumentElement, "wsa:Action")
    RemoveTag(envelope.DocumentElement, "wsa:MessageID")
    RemoveTag(envelope.DocumentElement, "wsa:To")
    Return SoapFilterResult.[Continue]
End Function

Private Sub RemoveTag(ByVal XE As System.Xml.XmlElement, ByVal TagName As String)
    For Each N As XmlNode In XE
        If N.ChildNodes.Count > 0 Then
            RemoveTag(N, TagName)
        End If
        If N.Name = TagName Then
            XE.RemoveChild(N)
        End If
    Next
End Sub

我发现,以除去这些寻址和安全报头中的最简单的方法是改变所述web服务的配置,而不是使用BasicHttpBinding WSHttp binding

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top