←Older revision | Newer revision→
The XMLHttp class is a member of the Microsoft XML Core Services (MSXML) library. The intent of the MSXML library is to provide developers with an api for working with XML documents.
The XMLHttp class provides client side support for communicating with servers via the Hypertext Transfer Protocol (HTTP). It enables the sending of a request to a HTTP server and the receiving of the corresponding response.
While the MSXML library was primarily developed as a means to work with XML documents, the XMLHttp class is not limited to sending and receiving XML. It can be used to send and receive any type of data.
XMLHTTP and Microsoft Access
The members of MSXML are made available to programming languages as a COM object. Because of this the XMLHttp class can be referenced from Visual Basic for Applications making it available to applications built using Microsoft Access.
Properties and methods
The following is a list of the properties and methods of the XMLHttp class commonly used when making a non asynchronous request from a VBA client.
Sending a request to a web service
The VBA code below sends a simple non asynchronous get request to the dictionary web service at http://services.aonaware.com/DictService/DictService.asmx. The service expects the query data to be appended to the url.
The Open method initializes the request, sets the request type to get, specifies the url of the web service and sets the call as not being asynchronous.
Specifying get means that any parameters added to the base url must be appended to the url as key=value pairs. The text of the url(request) is built in the code before the call to the Open method.
Providing a value for Async parameter of the Open method determines whether the code will wait for the subsequent call to Send to return before continuing. The default value of the Async parameter is false which means that the call is not asynchronous and the code will wait for the Send method to return. The Send method returns when all the data sent by the server is received.
Calling the Send method establishes the HTTP connection to the server, sends the request to the server and receives the response from the server. The status and statusText properties are also set by the Send method making them available after the Send method has returned.
Once the call to Send returns, the status property can be checked. A value of 200 indicates that the request to the server was successfully fulfilled. The status property can be used to determine what code to execute after the call to Send returns. A status value other than 200 does not necessarily mean that the responseText property will be empty. The server may send additional information about the error in the responseText.
Dim xhr As Object Dim webServiceURL As String Dim actionType As String Dim thisRequest As String Dim targetWord As String WebServiceURL = "http://services.aonaware.com/DictService/DictService.asmx/" actionType = "Define?word=" targetWord = "Marketplace" thisRequest = webServiceURL & actionType & targetWord
use late binding Set xhr = CreateObject("Microsoft.XMLHTTP")
xhr.Open "GET", thisRequest, False xhr.Send If xhr.status = 200 Then Debug.Print xhr.responseText MsgBox xhr.getAllResponseHeaders Else MsgBox xhr.status & ": " & xhr.statusText End If Set xhr = Nothing
The VBA code below sends a simple non asynchronous Post request to the dictionary web service at "http://services.aonaware.com/DictService/DictService.asmx/DefineInDict". The service expects the post data to be sent as key=value pairs in the same format that a post request from an HTTP form element would be sent.
When sending a post type request query parameters are not appended to the url sent to the server. Instead the query data is sent separately as the parameter to the Send method of the XMLHttp class.
Setting a type for the Content-Length header of a request is good practice when using Post with urlencoded data. Setting the request's Content-Length header is not strictly necessary, but some web servers do require it.
Setting the Content-Type to "application/x-www-form-urlencoded" tells the server that the posted data is in key=value pairs the way they would appear if posted from an html form.
Dim xhr As New XMLHTTP Dim webServiceURL As String Dim actionType As String Dim postData As String 'gcide is the service's id code for The Collaborative International Dictionary of English v.0.48 webServiceURL = "http://services.aonaware.com/DictService/DictService.asmx/" actionType = "DefineInDict" postData = "dictId=gcide&word=baritone" xhr.Open "POST", webServiceURL & actionType, False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xhr.setRequestHeader "Content-Length", Len(postData) xhr.Send postData If xhr.status = 200 Then MsgBox xhr.getAllResponseHeaders Debug.Print xhr.responseText Else MsgBox xhr.status & ": " & xhr.statusText Debug.Print xhr.responseText End If Set xhr = Nothing
Scenarios for use