Accessing Web Services from a Mobile Application

Web Services today have became a common technique. Windows CE-based devices, as usual, were several steps behind desktop systems. A couple of months ago, Microsoft finally re-released SP2 for CE.NET, promising significant performance improvements. Thus, recently mobile developers are also able to use most of Web Services’ cake. I have no intention to discuss the theory or concept of this technology here; you may freely find a lot of articles on the Web. Also, we won’t touch Web Services or Visual Studio.NET basics. What we’ll be focused on is what does it all mean in the case of Windows CE.

Creation of a C# Sample Project

As a test sample, we will create a simple client application that will use Amazon’s Web Services to query information about available books. So, let’s create a new “Smart Device Application” using Visual Studio.NET. The only option actually relevant for Windows CE is to create a C# project. That’s the simpliest way to develop Web Services-related applications. If you’ve used C++, you may either consider using the SOAP toolkit available in WinCE 4.x or, as the most complicated case, write a similar wrapper for SOAP classes for previous versions of Windows CE to assemble SOAP messages manually. The last case may become relevant when you need to achieve high performance benchmarks; for example, when developing an application that performs barcode scanning and then gets item details from a remote server. Our sample is simple, so all such details will be left out of this article’s scope.

Well, to be ready to start using Amazon Web Services, you should download Amazon Web Service Developer’s Kit from The last thing is to apply fora Developer’s token, and you’re completely set up. So, now, let’s add a Web Reference to our project to get a wrapper class for the Web Service. A WSDL file describing its API can be found here. Finally, we have a form to enter the author name and display search results Until now, all we’ve done is absolutely similar to desktop application development. Now, it’s time to take a look at the actual search code.

private void button1_Click(object sender, System.EventArgs e)
   Cursor.Current = Cursors.WaitCursor;

   AmazonSearchService srvc = new AmazonSearchService();
   WebProxy objProxy = new WebProxy (proxyAddr,proxyPort);
   objProxy.BypassProxyOnLocal = true;
   objProxy.Credentials = new NetworkCredential (user, password,
   srvc.Proxy = objProxy;
   GlobalProxySelection.Select = objProxy;

   AuthorRequest req = new AuthorRequest(); = SearchText.Text;
   req.devtag = DEVTAG;
   req.mode   = "books";
   req.type   = "lite";   = "1";

      ProductInfo pi = srvc.AuthorSearchRequest(req);
      for (int i = 0; i < pi.Details.Length; i++)
         string s = pi.Details[i].ProductName;
   catch(WebException ex)
      string message = ex.Message;
      HttpWebResponse response = (HttpWebResponse)ex.Response;
      if(null != response)
         message = response.StatusDescription;

   Cursor.Current = Cursors.Default;

As you see, first we should create an instance of a service object. If you’re connecting to the Internet or an intranet via a proxy, you should also define proxy settings. There was a bug in the CE.NET Web Client; it doesn’t look into the credentials of a global WebProxy object to authenticate it to the proxy server. For a workaround, you may define it directly, as shown in the above sample. All the rest of the code proceeds an actual call to Web Service and gets the search result from the remote server. If you were developing Web Service consuming applications for a PC, that’s the same story here. Like at Microsoft’s presentations, just do a Copy/Paste, and it’ll work.

Let’s now consider another important aspect of online applications. Your program should verify that the network connection is alive; in other words, requested network resources are available during the application’s lifetime. CE.NET framework has no classes to notify applications in response to changes in the network connectivity status. Therefore, mobile apps need to implement their own algorithms of polling active connections and possibly providing caching functionality. The simplest way to verify whether a device is connected to the network is to use a System.Theading.Timer object. When Tick Event occurs, you may use WebRequest and HttpWebResponse objects to send a HTTP GET request to the URL where the Web Service is located and receive an answer. If the status code of the response is equal to HttpStatusCode.OK, the connection to remote server is valid:

   // Somewhere in the code
   timer1.Interval = 1000;
   timer1.Enabled = true;
   // In responase to Tick event of timer
   WebRequest req = WebRequest.Create(url);
   HttpWebResponse result = (HttpWebResponse)req.GetResponse();
   if ( result.StatusCode != HttpStatusCode.OK )
      // Network connection was lost or remote server is
      // unavailable or some other HTTP error occurred

Actually that’s all you need to start successfully using Web Services on CE.NET mobile devices. If performance issues aren’t your headaches, you may just move your C# code to a mobile environment, obviously taking into account all existing restictions. For those of you who develop mobile business applications working with heavy data, C# can’t offer a sufficient solution. Therefore, our old buddy C++ is coming from behind the scene.

More by Author

Must Read