VBScript tip: Web service client

December 3, 2014 | Reliance SCADA

In applications with Reliance as a part of a bigger whole, there are often requests for data exchange with other systems. For information on how to exchange data with third-party applications, read the Data Exchange Methods manual, which is part of the Reliance installation. One of these methods is a Web service. It is a system of type client–server intended for data exchange between two computers via SOAP. SOAP (Simple Object Access Protocol) is a protocol for exchanging XML-based messages over a network using HTTP.


If the Reliance system is the data source, you can take advantage of the fact that the data servers (Reliance Server and Reliance Control Server) can work as the Web service's server and provide data to clients. You can use the examples of client programs in Java, Delphi (Object Pascal), VB.NET, and Android (Java), which are part of the Reliance installation, to create your own client program.


If another program is to be the data source (the Web service server) and Reliance is to be the Web service client, using objects for XML and HTTP is one of the ways to connect to the server:



' Namespaces.
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "http://www.w3schools.com/webservices/"
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"

' The URL of the Web service.
Dim URL
URL = "http://www.w3schools.com/webservices/tempconvert.asmx"

' The URL of the CelsiusToFahrenheit operation (function).
Dim Operation_CelsiusToFahrenheit
Operation_CelsiusToFahrenheit = "http://www.w3schools.com/webservices/CelsiusToFahrenheit"

' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param

' Creates an XML DOM object.
Set DOM = CreateObject("MSXML2.DOMDocument.6.0")

' Creates the main elements.
Set Envelope = DOM.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
DOM.appendChild Envelope
Set Body = DOM.createElement("soap:Body")
Envelope.appendChild Body

' Creates an element for the CelsiusToFahrenheit function.
Set Operation = DOM.createNode(1, "CelsiusToFahrenheit", NS)
Body.appendChild Operation

' Creates an element for the Celsius parameter (passes a value of 33 °C).
Set Param = DOM.createNode(1, "Celsius", NS)
Param.Text = "33"
Operation.appendChild Param

' Releases the objects.
Set Param = Nothing
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing

' Creates an XML HTTP object for sending a request.
Dim XMLHTTP
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", URL, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
XMLHTTP.setRequestHeader "SOAPAction", Operation_CelsiusToFahrenheit
' Sends the request.
XMLHTTP.send DOM.xml

' Loads the response to the DOM object.
DOM.LoadXML XMLHTTP.responseXML.xml

' Releases the object.
Set XMLHTTP = Nothing

' XML DOM objects.
Dim NodeList, Element

' Searches for the CelsiusToFahrenheitResult object, which contains the value in degrees Fahrenheit.
Set NodeList = DOM.getElementsByTagName("*")
For Each Element in NodeList
  If Element.tagName = "CelsiusToFahrenheitResult" Then
    MsgBox "Fahrenheit: " & Element.Text
    Exit For
  End If
Next

' Releases the objects.
Set Element = Nothing
Set NodeList = Nothing
Set DOM = Nothing

Fahrenheit

33 degrees Celsius is equal to 91.4 degrees Fahrenheit. The value was acquired using a Web service running on www.w3schools.com.


Note: Each Web service will have a different URL, different namespaces, and different names and URLs of operations. To connect to a Web service, this information must be adjusted accordingly.

Reliance Earth Icon

Top