【问题标题】:classic asp and soap response - how to handle changing prefixes in namespace?经典的 asp 和 soap 响应 - 如何处理命名空间中更改的前缀?
【发布时间】:2018-11-06 08:29:12
【问题描述】:

我继承了一个使用 web 服务的经典 asp 页面,这是我相对较新的东西。请求和响应工作正常,但我注意到前缀被添加到 wsdl 和我阅读它之间的某个名称空间。所以命名空间<FirstName> 将变成<ns2:FirstName> 或有时<ns3:FirstName> 或有时只是<FirstName>。它是完全相同的元素,但前缀经常变化。
我被告知我正在阅读“原始”响应,我需要过滤掉这些额外生成的前缀,但我不知道如何。 这是我如何使用 Web 服务的代码 sn-p(为了保持简单,我们对一些内容进行了修改):

Dim oXmlHTTP, objxml, SOAPRequest, SOAPResponse

    Set oXmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    oXmlHTTP.open "POST", "http://something.com/something/services/something?wsdl", False 
    oXmlHTTP.setRequestHeader "Content-Type", "text/xml; charset=UTF-8" 

    SOAPRequest = _
    "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:head='http://header.something.com" xmlns:ser='http://service.something.com' xmlns:add='http://service.something.com/domain/address'>" &_
       "<soapenv:Body>" &_
          "<ser:getInfo>" &_
             "<add:ID>123</add:ID>" &_
          "</ser:getInfo>" &_
       "</soapenv:Body>" &_
    "</soapenv:Envelope>"

    On Error Resume Next
    oXmlHTTP.send SOAPRequest   

    If Err.Number Then 
        Response.Write("Error: " & Err.Description)
        Err.Clear 
    Else 
        SOAPResponse = oXmlHTTP.responseText

    End If 
    On Error Goto 0 

    If LEN(SOAPResponse) > 0 then
        Set objxml = Server.CreateObject("MSXML2.DOMDocument.3.0")
        objxml.async = False
        objxml.load (oXmlHTTP.responseXML)      

        If objxml.parseError.errorCode <> 0 Then
             Response.Write("XML error")
        End If

        'This is where the "error" will occur, as the <Firstname> sometimes isn't recognized, and I must search for e.g. <ns2:Firstname> instead
        Set nodeList = objxml.getElementsByTagName("FirstName")
        SizeofObject = nodeList.length-1

        For i = 0 To (SizeofObject)  
            Response.Write ("Name: " & objxml.getElementsByTagName("FirstName").item(i).Text)
        Next    


    End If
        Set oXmlHTTP = Nothing 
        SOAPRequest = ""
        SOAPResponse = ""

如何忽略这些可能出现的“随机”前缀?在经典 asp 中使用 web 服务调用是否有更好的做法? (现在不能选择更改为 .NET)。任何帮助表示赞赏。

【问题讨论】:

    标签: soap asp-classic wsdl


    【解决方案1】:

    您可以使用 VBScript 的 Replace 函数将 Firstname 标记更改为 &lt;Firstname&gt; 吗?

    例如

    Set objxml = Server.CreateObject("MSXML2.DOMDocument.6.0")
    objxml.async = False
    Dim MyResponse
    MyResponse = oXmlHTTP.responseXML
    MyResponse = Replace(MyResponse,"<ns2:FirstName>","<FirstName>")
    MyResponse = Replace(MyResponse,"<ns3:FirstName>","<FirstName>")
    objxml.load (MyResponse) 
    

    注意,我通常建议调用 MSXML 的版本 6 而不是版本 3 - 它是最新版本

    【讨论】:

    • 这可行,但如果有一种“正确”的方式来处理这个问题,我会更感兴趣,我会完全避免这些生成的前缀。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多