dcsimg
January 23, 2017
Hot Topics:

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

  • December 20, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Receiving a Response as an XML DOM Document

Now, you can modify the previous sample to work with IXMLDOMDocument. The changes are really minimal:

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 )
   {

      CComQIPtr<IXMLDOMDocument,&__uuidof(IXMLDOMDocument)>
          spResponseXMLDoc;
      hr = spXMLHTTP->get_responseXML((IDispatch**)&spResponseXMLDoc);
      CComQIPtr<IXMLDOMNode,&__uuidof(IXMLDOMDocument)> spNode;
      hr = spResponseXMLDoc->
           selectSingleNode(_T("//GeneralInfo/Name"),&spNode);
      if( spNode != NULL )
      {
         CComBSTR bstrText;
         hr = spNode->get_text(&bstrText);
         TRACE(L"Text is %s\n",bstrText);
      }
      ...
   }
}

The red lines fully explain themselves. Just declare a XML DOM Document object instance, and you're set up. To avoid annoying stuff with COM objects declaration, creation, and releasing, all samples use smart pointers available, for example, in ATL CE. As a result, the code snippet obtains the initialized instance of the IXMLDOMDocument interface. The last action it does is running an XPath query to get some node text value.

Calling a Web Service via XMLHTTP

And finally, here's how to communicate with a Web Servce by using an XMLHTTP object. Web Services get their data upon using a "POST" method, so you need to take some additional steps. That is not too complicated, but it takes several lines of code:

static TCHAR* g_lpszEndpointURL = _T("http://some.server.com/
                                      testservice");

static TCHAR* g_lpszSOAPReq = _T(
"<?xml version='1.0' encoding='utf-8'?>
"<soap:Envelope "
"   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
"   xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
"   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/'>"
"  <soap:Body>
"    <UserLogin xmlns='http://tempuri'>"
"      <UserName>%s</UserName>"
"      <Password>%s</Password>"
"    </UserLogin>"
"  </soap:Body>"
"</soap:Envelope>");

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("POST")), g_lpszEndpointURL,
                        vAsync, vUser, vPwd);

   hr = spXMLHTTP->setRequestHeader(_T("Content-Type"),
                                    _T("text/xml"));
   hr = spXMLHTTP->setRequestHeader(_T("SOAPAction"),
                                    _T("'http://tempuri/UserLogin'"));

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

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

   if ( nStatus == 200 )
   {
      CComQIPtr<IXMLDOMDocument,&__uuidof(IXMLDOMDocument)>
         spResponseXMLDoc;
         hr = spXMLHTTP->get_responseXML((IDispatch**)
                                         &spResponseXMLDoc);

      CComQIPtr<IXMLDOMNode,&__uuidof(IXMLDOMDocument)> spNode;
      hr = spResponseXMLDoc->
           selectSingleNode(_T("some XPath query"),&spNode);
      if( spNode != NULL )
      {
         CComBSTR bstrText;
         hr = spNode->get_text(&bstrText);
         TRACE(L"Text is %s\n",bstrText);
      }
      ...
   }
}

In comparison with the previous sample, all you have changed is just a couple of lines marked in red. The request is opened with the "POST" method. Then, the sample code defines the values of the "Content-Type" and "SOAPAction" headers and request body. After receiving a successful response, data can be handled as needed.

Conclusion

I have discussed several simplified samples that give you a clue for implementing your own XMLHTTP-based modules. The usage of XMHTTP is covenient enough, so you can support it without too serious a headache. If you are not required to provide a sophisticated solution, it may be the best way to communicate with a remote HTTP host. Good luck!

About the Author

Alex Gusev started to play with mainframes in the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. Now, he works at an international retail software company as a team leader of the Mobile R department, making programmers' lives in the mobile jungles a little bit simpler.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel