October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Accessing Web Services from a Mobile Application

  • March 2, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Unfortunately, Pocket PC 2003 SDK doesn't support SOAP the Toolkit yet, even though it's from the WinCE 4.2 family. So, if you need to use it there (or on Pocket PC 2000/2002 with WinCE 3.x), you should implement several classes similar to those noted above to handle SOAP protocol issues manually. It seems to be not so awful as you may guess. All you actually need is to be familiar with the SOAP protocol and HTTP communications. Well, and XML also, to proceed the parsing of the received result. Such a manual solution may be ideal from several points of view. The compact framework still has bugs in its WebClient, which lead to troubles in case of complex networks with proxy server authentication and so forth. Next, you don't need to be well versed in CF.NET. Your application will work on almost all recent WinCE-powered devices. And finally, you will definitely achieve better performance and flexibility with C++ code in comparison with all other languages. Well, you will have to pay for it by some additional amount of coding. To evaluate this "override," look at the next code sample. It does not pretend to be an example of good programming technique, but shows a very simple example of using the same "AuthorSearchRequest" method of Amazon Web Services.

void CAmazonWScppDlg::OnButtonSearch()
{
   HRESULT hr = 0;
   CSoapWriter    objSoapWriter;
   CSoapReader    objSoapReader;
   CSoapConnector objSoapConnection;

   UpdateData();
   BeginWaitCursor();

   hr = objSoapConnection.Connect(L"http://soap.amazon.com/onca/
                                           soap3");
   if (FAILED (hr))
   {
      AfxMessageBox(L"Failed to connect to URL");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.StartEnvelope ()))
   {
      TRACE (L"StartEnvelope\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.StartBody ()))
   {
      TRACE (L"StartBody\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.StartElement
      (L"AuthorSearchRequest", TEMPURI)))
   {
      TRACE (L"StartElement\n");
      goto CleanUp;
   }


   if (FAILED (hr = objSoapWriter.StartElement (L"AuthorRequest")))
   {
      TRACE (L"StartElement\n");
      goto CleanUp;
   }

   objSoapWriter.WriteElementString(_T("author"),m_sAuthor);
   objSoapWriter.WriteElementString(_T("devtag"),
                                    _T("D2TBTDKMUHSOS9"));
   objSoapWriter.WriteElementString(_T("mode"),_T("books"));
   objSoapWriter.WriteElementString(_T("type"),_T("lite"));
   objSoapWriter.WriteElementString(_T("page"),_T("1"));

   if (FAILED (hr = objSoapWriter.EndElement (L"AuthorRequest")))
   {
      TRACE (L"EndElement\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.EndElement
      (L"AuthorSearchRequest")))
   {
      TRACE (L"EndElement\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.EndBody ()))
   {
      TRACE (L"EndBody\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.EndEnvelope ()))
   {
      TRACE (L"EndEnvelope\n");
      goto CleanUp;
   }

   if (FAILED (hr = objSoapWriter.FinalizeSoap ()))
   {
      TRACE (L"FinalizeSoap\n");
      goto CleanUp;
   }

   hr = objSoapConnection.Invoke(objSoapWriter,
                                 L"AuthorSearchRequest");
   if ( FAILED(hr = objSoapReader.GetSoapResponse(objSoapConnection,
                                                  m_sResult)) )
   {
      TRACE (L"FinalizeSoap\n");
      goto CleanUp;
   }
   UpdateData(0);

CleanUp:
   EndWaitCursor();
   TRACE(L"Exit\n");
}

This sample utilizes some simple implementation of CSoapWriter, CSoapReader, and CSoapConnector the author found a while ago somewhere on the Web (probably at the Microsoft site) and redesigned and rewrote to do its job in an MFC environment. You may find the sample project here and use it for you own risk and fun. But basically, as you may see from the sample code, there is nothing complicated there. Well, that's just a simple test application to illustrate the main idea, so it does not implement any significant error checking or connection status verification. The connection status may be checked by a timer too, with various methods, such asa 'dummy' HTTP GET request or ping. But the most important thing: It just works!

And, last but not least, in some cases you may also use the REST protocol (XML over HTTP). Amazon Web Services supports REST along with SOAP access:

void CAmazonWScppDlg::OnButtonSearch2()
{
   CSoapReader    objSoapReader;
   CSoapConnector objSoapConnection;

   UpdateData();
   BeginWaitCursor();

   HRESULT hr = objSoapConnection.Connect(L"http://xml.amazon.com/
                                                   onca/xml3");
   if (FAILED (hr))
   {
      AfxMessageBox(L"Failed to connect to URL");
      goto CleanUp;
   }

   hr = objSoapConnection.InvokeXml
        (L"t=Alex&dev-t=D2TBTDKMUHSOS9&AuthorSearch=" +
         m_sAuthor + L"&mode=books&type=lite&page=1&f=xml");
   if ( FAILED(hr = objSoapReader.GetSoapResponse(objSoapConnection,
                                                  m_sResult)) )
   {
      TRACE (L"FinalizeSoap\n");
      goto CleanUp;
   }
   UpdateData(0);

CleanUp:
   EndWaitCursor();
   TRACE(L"Exit\n");
}

It looks even simplier than SOAP, but it's less powerful. Actually, it's like HTTP GET versus HTTP POST arguing. Use what is more suitable for you, that's all. Now, you hopefully may quickly and successfully start developing your own Web-Service-consumer applications. Just do it!

About the Author

Alex Gusev started to play with mainframes at 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.

 

 


Sitemap | Contact Us

Rocket Fuel