Source: XMLOverHTTP.frm

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Source: XMLOverHTTP.frm

This Visual Basic code example consists primarily of two subroutines: MakeRequest and ProcessRepsonse.

In MakeRequest, we construct a request URL based on user input, connect to the specified server, and issue the request. If the request is synchronous, we then call ProcessReponse. Otherwise, we exit the routine and the onreadystatechange event handler calls ProcessResponse when the response becomes available. In the ProcessResponse routine, we extract the requested information from the server and display it in a text box on the form.

Visual Basic Source File (XMLOverHTTP.frm)

Public oHttpReq As XMLHTTP50
Public oXMLDoc As DOMDocument50

Private Sub Command1_Click()
    MakeRequest (True)
End Sub

Private Sub MakeRequest(ByVal isAsync As Boolean)
    Set oHttpReq = New XMLHTTP50
    Dim xhrHandler As myHttpRequestHandlers

    If isAsync = True Then
        Set xhrHandler = New myHttpRequestHandlers
        
        ' Set a readyStateChange handler.
        oHttpReq.OnReadyStateChange = xhrHandler
    End If
    
    ' Construct the URL from user input.
    url = Text1.Text
    If Text2.Text <> "" Then
        url = url + "?SearchID=" + Text2.Text
    End If
    ' Clear the display.
    Text3.Text = ""
    
    ' Open a connection and set up a request to the server.
    oHttpReq.open "GET", url, isAsync
        
    ' Send the request to the server.
    oHttpReq.send
    
    ' In a synchronous call, we must call ProcessResponse. In an 
    ' asynchronous call, the OnReadyStateChange handler calls 
    ' ProcessResponse.
    If isAsync = False Then
        ProcessResponse
    End If
End Sub
Public Sub ProcessResponse()
    ' Receive the response from the server.
    Set oXMLDoc = oHttpReq.responseXML
    
    ' Display the server response to the user.
    Set oNode = oXMLDoc.selectSingleNode("//phone")
    If oNode Is Nothing Then
        Text3.Text = "Requested information not found."
    Else
        Text3.Text = oNode.Text
    End If
End Sub

Private Sub Form_Load()
    Text3.Text = ""
    Text1.Text = "http://localhost/sxh/contact.asp"
    Text2.Text = "John Doe"
End Sub

To add XMLOverHTTP.frm to the project and set the controls

  1. Add three TextBox controls to the form. The Text1 control will be used for the URL of the contact look-up service. Text2 will allow a user to specify the name of a contact whose telephone number will be queried. Text3 is used to display the query result.
  2. Add a label for each text box and change the captions to "Service:", "Name:", and "Phone:", respectively.
  3. Add a command control (Command1) that will start a request when a user clicks on it.
  4. Arrange the controls in the form as shown below:

  5. Copy the code listing above. Paste it into the Visual Basic code editor as the form_load subroutine, replacing any code fragments that are already there.

Next, we'll add a class module as the handler to the onreadystatechange event of the IXMLHTTPRequest object.