Web Services can achieve a variety of things for you. For example, you might want to convert a certain value to another. In the context of KTM, you may need to verify a certain number against a database, and that service is being provided by a Web Service. Well, given the right service you could even export document metadata from KTM, including the image. In general, there are two “large types” of webservices:
- Web Services using XML messages following the SOAP (Simple Object Access Protocol) standard, and
- Web Services using simple REST (Representational State Transfer), not requiring XML or WSDL.
While the latter one are more suited for ad-hoc-scenarios and are easy to consume, SOAP offers a variety of advantages (such as following a contract, rigid type-checking, etc). Here are two examples on how to consume Web Services out of KTM, both times using a script locator as the container for the result.
Example 1: RESTful Web Services Call using http GET
Private Sub SL_WS_REST_LocateAlternatives(ByVal pXDoc As CASCADELib.CscXDocument, ByVal pLocator As CASCADELib.CscXDocField)
' *** example 1: very simple RESTful webservice with GET
Dim uri As String
Dim response As String
'
Dim http As Object
Set http = CreateObject("MSXML2.ServerXMLHTTP")
uri = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/17"
http.Open "GET", uri, False
http.Send
response = http.responseText
Set http = Nothing
pLocator.Alternatives.Create
pLocator.Alternatives(0).Text = Xml_SelectSingleNode(response, "//LASTNAME")
pLocator.Alternatives(0).Confidence = 1
End Sub
Example 2: Web Service Call using SOAP
Private Sub SL_SW_SOAP_LocateAlternatives(ByVal pXDoc As CASCADELib.CscXDocument, ByVal pLocator As CASCADELib.CscXDocField)
' *** example 2: webservice using SOAP
Dim dom As Object
Dim http As Object
Set dom = CreateObject("MSXML2.DOMDocument")
Set http = CreateObject("MSXML2.XMLHTTP")
Dim uri As String
Dim soapAction As String
Dim xml As String
Dim parameter As String
Dim response As String
uri = "http://www.w3schools.com/webservices/tempconvert.asmx"
soapAction = "http://www.w3schools.com/webservices/CelsiusToFahrenheit"
parameter = "13"
xml = "" & _
"" & _
"" & _
"" & _
"" & parameter & "" & _
"" & _
"" & _
""
' load the xml
dom.async = False
dom.loadXML xml
' send POST request (= "open" webservice)
http.Open "POST", uri, False
' create approproate SOAP headers
http.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
http.setRequestHeader "SOAPAction", soapAction
' send xml
http.send dom.XML
' fetch the response
response = http.responseText
Set dom = Nothing
Set http = Nothing
' parse result
pLocator.Alternatives.Create
pLocator.Alternatives(0).Text = Xml_SelectSingleNode(response, "//CelsiusToFahrenheitResult")
pLocator.Alternatives(0).Confidence = 1
End Sub
Parsing XML
Here’s a small helper function to parse XML files using MSXML.
Public Function Xml_SelectSingleNode(xml As String, xpathQuery As String) As String
Dim doc As Object
Dim nodeList
Dim node
Dim success As Boolean
Set doc = CreateObject("MSXML2.DOMDocument")
success = doc.LoadXml(xml)
If success Then Return doc.selectSingleNode(xpathQuery).Text
Return ""
End Function