Kotan Code 枯淡コード

In search of simple, elegant code

Menu Close

Accessing Web Services from Windows Phone 7

I’ve said many times before that the Windows Phone 7 learning curve is actually pretty small assuming you already know Silverlight. One of the hardest things to get used to with Silverlight is remembering which pieces of the .NET Framework are not available to developers from Silverlight and WP7.

One area that is significantly limited from it’s larger desktop counterpart is networking. For obvious reasons, the number of ways in which Silverlight can initiate outbound network connections is capped, including the fact that developers do not have access to the WebRequest class.

In Silverlight and Windows Phone 7 development, there are only to ways to access web services: Using WCF or using the System.Net.WebClient class. There are a plethora of resources available on the web for both of these methods, including probably nearly a dozen different books on WCF programming.

Assuming you aren’t using WCF to talk to a back-end web service, then you’re probably going to want to consume data over regular HTTP. To do this, you use the WebClient class. Some of us remember the good old early days of .NET programming when the use of this class was commonplace.

Here’s some code that downloads the contents of a web page as a string and prints that string to the debug log and it does it all asynchronously:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Diagnostics;

namespace WindowsPhoneApplication1
{
 public partial class MainPage : PhoneApplicationPage
 {
 // Constructor
 public MainPage()
 {
 InitializeComponent();

 WebClient wc = new WebClient();
 wc.DownloadStringAsync(
    new Uri("http://my.serviceapp.com/my/webservice"));
 wc.DownloadStringCompleted +=
    new DownloadStringCompletedEventHandler(
      wc_DownloadStringCompleted);
 }

 void wc_DownloadStringCompleted(object sender,
    DownloadStringCompletedEventArgs e)
 {
     Debug.WriteLine("Web service says: " + e.Result);
 }
 }
}

Finally, the main methods we need to be concerned with on the WebClient class are:

  • UploadData and UploadDataAsync – This is what you’ll use to POST or PUT data to a RESTful (or less-than-RESTful depending on which API you’re talking to…) web service.
  • UploadString and UploadStringAsync – Rather than sending byte arrays, this lets you push a simple string. This comes in very handy if you’re talking XML over the wire.
  • DownloadData and DownloadDataAsync – Downloads (GET) data from a web URL.
  • DownloadString and DownloadStringAsync – Downloads data from a web URL and that data is returned to the developer as a simple string. Again, this is a really handy shortcut for talking XML to remote endpoints.

There are also a bunch of useful properties, including properties like Credentials that help you authenticate against web services and you can access the HTTP headers (many public web services make use of this facility) using the Headers property.

Stay tuned, this is just the first post of many in some WP7 posts that I’ve had lying around for a while and needed to get out of my system. Long story short: armed with System.Net.WebClient you can talk HTTP to any web service in the cloud. In subsequent posts, I’ll show some more techniques for dealing with things like XML, RSS, etc.

Check out Part 2 of this series, where I discuss parsing XML on WP7.