Web Services - Microsoft Visual Studio 6.0 Example

This example is written in Visual Basic 6.0 and consists of the code for a form containing a TextBox (txtID), a label (lblResult) and two buttons (cmdOK and cmdCancel). A reference to the Microsoft SOAP 3.0 Toolkit has been added to the project and the code calls the web service defined in the the WSDL file http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl.

Note

The code in this example could also be used in Microsoft Office 2000 and earlier.

Option Explicit

' Form-level constants (available to all routines in this form).
Private Const conWSDL_URL = "http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl"
Private Const conSERVICE As String = "WSTestService"
Private Const conPORT As String = "GETADDRESSPort"
' Form-level variables (available to all routines in this form).
Private fobjWSTest As SoapClient30

Private Sub Form_Load()
    Set fobjWSTest = New SoapClient30
    With fobjWSTest
        ' Initialise the SOAP client object to use the web service.
        .MSSoapInit conWSDL_URL, conSERVICE, conPORT
        ' Use the IE proxy server, if any.
        .ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
        .ConnectorProperty("EnableAutoProxy") = True
    End With
End Sub

Private Sub cmdOK_Click()
    Dim companyName, addressLine, telephone, zip, id As String
    companyName = ""
    addressLine = ""
    telephone = ""
    zip = ""
    id = txtID.Text
    On Error Resume Next
    fobjWSTest.GetAddress id, companyName, addressLine, telephone, zip
    If Err.Number = 0 Then
        lblResult.Caption = companyName & vbCrLf & addressLine & vbCrLf _
            & zip & vbCrLf & telephone
    Else
        Debug.Print "Web Service error!"
    End If
    On Error GoTo 0
End Sub

Private Sub cmdCancel_Click()
    Unload Me
End Sub

The URL of the WSDL file and the names of the web service and the port are defined as form-level constants. Similarly a form-level variable (fobjWSTest) is declared to hold a reference to a SOAP client object that will provide access to the web service methods.

When the form loads, an instance of the SOAP client is created and stored in fobjWSTest. This is then initialised to access the web service, this gives it methods corresponding to those of the web service that we can use in our code. It is also set up to use any proxy server configured in Internet Explorer.

Note

The SOAP client object will only be given the web service methods when the program is run. This means that there will be indication of what methods are available while you are writing your code, so you will have to follow the web service's documentation carefully.

The cmdOK button click routine declares and initialises variables in which values will be passed to and returned by the GetAddress web service method (the id variable is set to a company ID that the user has entered into the txtID control). The method is then called and the results displayed in the lblResult control. Code is included to handle any errors.