VB.NET Web Services

VB.NET Web Services

This article describes how to use a VB.NET script to communicate directly with HEAT LiveTime via Web Services. When using .NET, the address of all the service names, needs to be prefixed with an _, to utilize the newer .NET interfaces.

The service’s endpoint address will be the following:
http://HOST_NAME/Service/WebObjects/Service.woa/ws/_SERVICE

As such, the XML Descriptor of the service (WSDL) can be found here:
http://HOST_NAME/Service/WebObjects/Service.woa/ws/_SERVICE?wsdl

The methods for adding web services differs slightly between versions but the general steps are the same. Using Visual Basic Express 2010, to add these into a solution, go to Project > Add Service Reference, Select Advanced and then Add Web Reference. (A common mistake is to omit those last 2 buttons, which are required in the 2010 version). This can be repeated for each service URL required.

The web services commands that rely on authentication, require that the user logs in first using the Connect command and then subsequent commands need to be part of the same session which was instigated by a successful Connect command. This is done by ensuring the JSESSIONID cookie that is set up when the Connect succeeds is then then passed back to the server with each command required.

Here is a Windows Form example written in Visual Basic 2010 using .NET 4.0 as the reference basis, although this should work with earlier versions too. Using the initial form that is generated, a button was placed on the form to call that example code. It illustrates logging in, storing the JSESSIONID cookie, adding that cookie to the next command before sending the next command.

Imports System.Net

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim _Uri As New Uri("http://westdemo.livetime.com/Service")   'This should match the address of the web services references added to the solution
        Dim _InitialCookieContainer As New CookieContainer
        Dim _CookieContainer As New CookieContainer
        Dim _Cookie As New Cookie
        Dim _LoginSuccess As Boolean = False

        'Login

        Dim LTAuth As New LTAuthenticate._Authenticate   'LTAuthenticate is the name given to the Authenticate web service of HEAT LiveTime in the solution
        Dim _auth_response() As LTAuthenticate.WsNameValuePair
        LTAuth.CookieContainer = _InitialCookieContainer
        _auth_response = LTAuth.connect("super", "super")   'Login details

        For Each _response In _auth_response
            If _response.key = "success" And _response.value = "true" Then
                _LoginSuccess = True
            End If
        Next

        'If successfully logged in, store the JSESSIONID for subsequent use.
        'Alteranatively, all the initial cookies can be referenced without extracting just the required JSESSIONID one but this example shows what is required in HEAT LiveTime.

        If _LoginSuccess Then
            _Cookie = _InitialCookieContainer.GetCookies(_Uri).Item("JSESSIONID")
            _CookieContainer.Add(_Cookie)  'Setup a separate CookieContainer with just the required JSESSIONID cookie for subsequent requests
        Else
            MessageBox.Show("Login Failed")
            Application.Exit()
        End If

        'Example to find customers

        Dim LTCust As New LTCustomer._Customer   'LTCustomer is the name given to the Customer web service of HEAT LiveTime in the solution
        LTCust.CookieContainer = _CookieContainer   'The Customer commands are to use the CookieContainer created above - Alternatively, in this case, it could also use the initial one here, as mentioned above.

        Dim _cust_response() As LTCustomer.WsNameValuePair

        _cust_response = LTCust.findCustomer("", "", "")   'Calling findCustomer with email, first name, last name

        'Parse the result to a message box, displaying 4 at a time

        Dim _pair As LTCustomer.WsNameValuePair
        Dim _total_response As String = ""
        Dim _count As Integer
        _count = 0
        For Each _response In _cust_response
            If TypeOf (_response.value) Is String Then
                If _response.key = "message" Then
                    _total_response = _response.value
                End If

            ElseIf Not (_response.key = "success") Then
                _total_response = _total_response + _response.key + " : " + vbNewLine
                _count = _count + 1
                For Each _pair In _response.value
                    _total_response = _total_response + _pair.key.ToString + " : " + _pair.value.ToString + vbNewLine
                Next
                _total_response = _total_response + vbNewLine
            End If

            If _count >= 4 Then
                MsgBox(_total_response)
                _total_response = ""
                _count = 0
            End If
        Next
        If _count <> 0 Then
            MsgBox(_total_response)
        End If

    End Sub
End Class

If using another language (eg. C#) or type of .net application, the same principles apply so the Visual Basic Windows Form example can be used as a guide.