CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Internet & Networking >> Internet Protocols >> XML


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

Alex Gusev (view profile)
December 21, 2004

Go to page: 1  2  Next

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


(continued)




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
Methods
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.

Go to page: 1  2  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Article title different ? - Pritam Pal (12/21/2004)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)