【问题标题】:SOAP Service call - Getting error Illegal Request format for elementSOAP 服务调用 - 获取元素的错误非法请求格式
【发布时间】:2019-09-16 15:11:29
【问题描述】:

我们正在整合来自第三方的服务。他们最近升级了他们的服务,现在有了新的 wsdl,我不断收到“元素的非法请求格式”。

根据我的调查,问题似乎在于添加到主要元素上的 xmlns。如果我使用 SOAPUI 并从主元素中删除 xmlns,它可以工作,但是 Visual Studio 会根据 wsdl 中定义的内容自动添加它。

有趣的是,在他们以前的 wsdl 中,该服务可以与包含的 xmlns 一起使用,只有新的 wsdl 才会引发异常。

就 wsdl 而言,我所知道的是他们使用 JD 12 并手动创建了 wsdl,但经过比较,它看起来与旧版本相似,唯一不同的是 xmlns 中的名称。

这是视觉工作室创建的请求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <extractacccategElement xmlns="http://gna160ws/Management.wsdl/types/">
            <xSecurity>
                <timekey></timekey>
                <authkey></authkey>
                <publkey></publkey>
                <version>1.x</version>
            </xSecurity>
            <xRequest1>
                <supplierno></supplierno>
            </xRequest1>
        </extractacccategElement>
    </s:Body>
</s:Envelope>

由于“xmlns="http://gna160ws/Management.wsdl/types/"” 导致了问题。

回复:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <env:Header/>
   <env:Body>
      <srvc:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
         <srvc:result>
            <response1Out>
               <origin>gna160.extractacccategElement</origin>
               <invsql>1200</invsql>
               <message>Illegal Request format for extractacccategElement.</message>
            </response1Out>
         </srvc:result>
      </srvc:extractacccategElementResponse>
   </env:Body>
</env:Envelope>

当提交相同的请求,但没有 xmlns,我得到一个有效的响应。示例:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <extractacccategElement>
            <xSecurity>
                <timekey></timekey>
                <authkey></authkey>
                <publkey></publkey>
                <version>1.x</version>
            </xSecurity>
            <xRequest1>
                <supplierno></supplierno>
            </xRequest1>
        </extractacccategElement>
    </s:Body>
</s:Envelope>

回复:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
    <ns0:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
       <ns0:result>
          <ns0:acccategOut>
                <ns0:invsql>0</ns0:invsql>
                <ns0:message>Success</ns0:message>
                <ns0:origin>gna160pkg.ExtractAccCateg</ns0:origin>
                <ns0:stage>1</ns0:stage>
                <ns0:acccategcode>A</ns0:acccategcode>
                <ns0:acccategname>AAA</ns0:acccategname>
          </ns0:acccategOut>
       </ns0:result>
    </ns0:extractacccategElementResponse>
    </env:Body>
</env:Envelope>

此外,如果我在 xmlns 之后添加一个限定符,那么它也可以工作?? 示例:

xmlns:hello="http://gna160ws/Management.wsdl/types/"

我一直在与他们这边的开发人员合作,但我们还没有发现问题。

如果有人可以帮助或指出正确的方向,将不胜感激。

【问题讨论】:

    标签: c# service wsdl


    【解决方案1】:

    您是否使用新的 WSDL 更新了您的服务引用?

    或者,您可以尝试 Rick Strahl 的 answer:覆盖以下函数以添加自定义命名空间。

    protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
    {
            writer.WriteStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
            writer.WriteAttributeString("xmlns", "oas", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            writer.WriteAttributeString("xmlns", "v2", null, "http://www.royalmailgroup.com/api/ship/V2");
            writer.WriteAttributeString("xmlns", "v1", null, "http://www.royalmailgroup.com/integration/core/V1");
            writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");            
    }
    

    Link他的完整文章

    【讨论】:

    • 是的,我确实更新了服务参考。感谢您提出上述答案,但是我有多个不同的服务,以上内容不会影响对不同服务的所有请求吗?
    • 另外,我不知道上面的方法是否可行,因为我需要从元素中完全删除 xmlns,或者在 xmlns 之后添加一个限定符,例如 :name
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2015-09-20
    • 2015-03-24
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多