Mastering Internet Programming on Mobile Devices: Data Exchange Using an XML HTTP Interface

Two previous articles, “Mastering Internet Programming on Mobile Devices: First Steps” and “Mastering Internet Programming on Mobile Devices: An Asynchronous Data Exchange,” covered various WinInet-based communications. WinInet does a lot of black jobs for the application programmer, providing a relatively simple way of data transmission to remote hosts. A recent article discovers another aspect of Internet communications; this is the XML HTTP interface available under Pocket PC 2003 and later.

An Overview of IXMLHTTPRequest

The latest versions of MS XML shipped with Pocket PC 2003-flavoured devices that expose the XMLHTTP object, which in turn allow exchanging and manipulating data in XML format. Compared to its desktop brother, it’s poorly documented in the WinCE help system. Hence, I’ll shortly run through IXMLHTTPRequest‘s methods and properties. It is summarized in the following table. I’ve used C++ syntax, but VB developers will find only a few differences.

Programming Element Desciption
HRESULT open(BSTR bstrMethod,BSTR bstrUrl,VARIANT varAsync,VARIANT bstrUser,VARIANT bstrPassword) Initializes an MSXML.XMLHTTP request and specifies the method, URL, and authentication information for the request
HRESULT setRequestHeader(BSTR bstrHeader,BSTR bstrValue) Defines the specific HTTP header
HRESULT send(VARIANT varBody) Sends an HTTP request to the server and receives a response
HRESULT getResponseHeader(BSTR bstrHeader,BSTR *pbstrValue) Retrieves the value of an HTTP header from the response body
HRESULT getAllResponseHeaders(BSTR *pbstrHeaders)) Retrieves the values of all the HTTP headers
HRESULT abort(void) Cancels the current HTTP request
Get Properties
HRESULT get_status(long *plStatus) Represents the HTTP status code returned by a request
HRESULT get_statusText(BSTR *pbstrStatus) Represents the HTTP response status text
HRESULT get_responseXML(IDispatch **ppBody) Represents the parsed response entity body as a DOM XML document
HRESULT get_responseText(BSTR *pbstrBody) Represents the response entity body as a string
HRESULT get_responseBody(VARIANT *pvarBody) Represents the response entity body as an array of unsigned bytes
HRESULT get_responseStream(VARIANT *pvarBody) Represents the response entity body as an IStream object
HRESULT get_readyState(long *plState) Represents the state of the request
Put Properties
HRESULT put_onreadystatechange(IDispatch *pReadyStateSink) Specifies the event handler to be called when the readyState property changes

So, all the business looks relatively easy.

Common Working Flow

Similar to other WinInet scenarios, XMLHTTP usually can be used as follows:

  • Define and initialize an IXMLHTTPRequest interface instance
  • Call an open method to set up a request method (for example, “GET”), URL, sync/async mode, user, and password
  • Optionally call a setRequestHeader method to set up all required headers
  • Call a send method to send a request to the remote host
  • Obtain a response in the desired form; in other words, as a string, XML document, and so forth, by calling the appropriate get_XXX method
  • Release the IXMLHTTPRequest object

Following the announced process, the following snippet shows these simple steps:

static TCHAR* g_lpszEndpointURL = _T("http://someserver/alex.xml");

CComQIPtr<IXMLHTTPRequest,&__uuidof(IXMLHTTPRequest)> spXMLhttp:
HRESULT hr = spXMLHTTP.CoCreateInstance(__uuidof(XMLHTTPRequest));
if ( SUCCEEDED(hr) )
   COleVariant vAsync,vUser,vPwd;
   vAsync = VARIANT_FALSE;
   hr = spXMLHTTP->open(CComBSTR(_T("GET")), g_lpszEndpointURL,
                        vAsync, vUser, vPwd);

   COleVariant vBody;
   hr = spXMLHTTP->send(vBody);

   long nStatus = 0;
   hr = spXMLHTTP->get_status(&nStatus);

   if ( nStatus == 200 )
      CComBSTR bstrString;
      hr = spXMLHTTP->get_responseText(&bstrString);

      //process it as needed

The preceding code just takes some XML file pointed to by the g_lpszEndpointURL parameter from the remote host. Obviously, you can use similar “GET” requests to receive some XML data from ASP pages. To call Web Services, the application should use the “POST” method. In this case, you will have to provide all SOAP enveloping manually. I will discuss it later in this article.

More by Author

Must Read