我正在尝试通过Excel 2010中的VBA中的SOAP请求检索一些信息。我以前没有使用过这个,但我确实做了一些搜索并调整了不同的代码来提出我认为应该工作的东西但是我一直遇到同样的错误(没有SOAPAction标头)。我在SoapUI中尝试了WSDL,它可以工作,所以我不确定我哪里出错了。
我提出的WSDL请求:
http://www.banxico.org.mx/DgieWSWeb/DgieWS?WSDL
我不断得到的错误:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode>
<faultstring>no SOAPAction header!</faultstring>
<detail>
<ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">SERVAP9</ns2:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
我正在使用的VBA代码:
Option Explicit
Sub SOAP()
'Set and instantiate our working objects
Dim Req As Object
Dim sEnv As String
Dim Resp As New MSXML2.DOMDocument60
Set Req = CreateObject("MSXML2.XMLHTTP")
Set Resp = CreateObject("MSXML2.DOMDocument.6.0")
Req.Open "Post", "http://www.banxico.org.mx/DgieWSWeb/DgieWS?WSDL", False
'Create SOAP envelope for submission to the Web Service
sEnv = sEnv & "<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ws=""http://ws.dgie.banxico.org.mx"">"
sEnv = sEnv & " <soapenv:Header/>"
sEnv = sEnv & " <soapenv:Body>"
sEnv = sEnv & " <ws:tiposDeCambioBanxico soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""/>"
sEnv = sEnv & " </soapenv:Body>"
sEnv = sEnv & "</soapenv:Envelope>"
' Send SOAP Request
Req.send (sEnv)
' Display results in MessageBox
'MsgBox Req.responseText
Resp.LoadXML Req.responseText
Debug.Print Req.responseText
'clean up code
Set Req = Nothing
Set Resp = Nothing
End Sub
答案 0 :(得分:0)
在这个块中:
'Create SOAP envelope for submission to the Web Service
sEnv = sEnv & "<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ws=""http://ws.dgie.banxico.org.mx"">"
sEnv = sEnv & " <soapenv:Header/>"
sEnv = sEnv & " <soapenv:Body>"
sEnv = sEnv & " <ws:tiposDeCambioBanxico soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""/>"
sEnv = sEnv & " </soapenv:Body>"
sEnv = sEnv & "</soapenv:Envelope>"
您应该在行<soapenv:Header/>
之后取出</soapenv:header>
或添加相应的</soapenv:Body>
。