Understanding Connectivity Offerings for Web and Data Services for the Windows Phone Platform


Most of the Windows Phone applications today interact with the web. There are hardly any applications that are purely local in nature; they interact with the web for functionality, data synchronization or at the least, application updates.

Developers should be aware of the connectivity offerings the Windows Phone platform provides so that they can choose the optimal way of interacting with the Internet via their application.

The Windows Phone platform supports three main connectivity options:

  • HTTP based requests
  • Web services
  • Data services

In this article, we will explore more about HTTP based requests.

HTTP Based Requests

Hypertext Transfer Protocol or HTTP in short, is the simplest networking protocol that all web browsers currently use for communicating.

Windows Phone platform provides a variety of built-in classes, which can be used in your application code.

WebClient Class: This class is used for sending and receiving data based on URI addresses.

HttpWebRequest class: This class is used for making HTTP based web requests. It is derived from the generic WebRequest class.

Let us take a look at an example of how we use the WebClient class in our Windows Phone application.

Start Visual Studio 2010 and create a C# project of type "Silverlight for Windows Phone" and choose the Windows Phone template.

Add a button and a textbox on the default page. Name the button as "WebClient Demo" and clear the context of the textbox.

Now wire up the click event of the button and add the following code to the click event handler function.

        private void button1_Click(object sender, RoutedEventArgs e)
           Uri serviceUri = new Uri("http://www.developer.com/icom_includes/feeds/developer/dev-open-10.xml");
           WebClient webClientInstance = new WebClient();
           webClientInstance.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientInstance_OpenReadCompleted

In the above snippet we are creating a URI to the RSS feed for www.developer.com articles.

We are also creating an instance of the web-client and wiring the event handler for the OnReadCompleteEvent of the WebClient instance.

We now have to create an event handler for OnReadCompleteEvent event.

       void webClientInstance_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            if (e.Error == null) // check for error
                Stream responseStream = e.Result;
                if (responseStream.CanSeek)
                    long a = responseStream.Length;
                    byte[] buffer = new byte[a];
                    responseStream.Read(buffer, 0, (int) a);
                    string str = System.Text.Encoding.UTF8.GetString(buffer, 0, (int)a);
                    textBox1.Text = str;

In the event hander function, we check to see if there was an error and if there was none, we can process the result. The result of the WebClient read request is a response stream, which can be read from to see the response.

We read the responseStream into a buffer and then at the end, we update the textbox with the contents of the response stream.

We find that the response is an HTTP GET response to our Web request.

----- HTTP Response
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<title><![CDATA[Developer.com Open Source]]></title>
<description><![CDATA[Developer.com Open Source]]></description>
<copyright>Copyright 1996-2011 Quin Street Corporation</copyright>
                     <title><![CDATA[Oracle Commits to Open Source Hudson at Eclipse]]></title>
                     <description><![CDATA[<p>Fork? What fork? Oracle re-affirms its open source commitment to the Hudson continuous integration project, moving it to Eclipse with multi-vendor support.</p>]]></description>
                     <pubDate>Wed, 04 May 2011 12:50:00 -0700</pubDate>
                     <title><![CDATA[Oracle Advances NetBeans for Java 7]]></title>
                     <description><![CDATA[<p>The NetBeans 7 release adds Java 7 and HTML 5 support, as well as new integration with WebLogic to the open source IDE.</p>]]></description>
                     <pubDate>Thu, 21 Apr 2011 14:23:00 -0700</pubDate>
                     <title><![CDATA[Using the New Python 3.2 Concurrent Programming Features]]></title>
                     <description><![CDATA[<p>A new module in Python 3.2 simplifies thread and process submission, results handling, synchronization of execution and worker threads/process pooling.</p>]]></description>
                     <pubDate>Wed, 13 Apr 2011 00:00:00 -0700</pubDate>
                     <title><![CDATA[Adobe Extends Flash Builder for Mobile and PHP]]></title>
                     <description><![CDATA[<p>Adobe updates Flex and Flash Builder with new releases that enable developers to develop for multiple mobile platforms including Android, Blackberry and even iOS.</p>]]></description>
                     <pubDate>Mon, 11 Apr 2011 15:40:00 -0700</pubDate>
                     <title><![CDATA[10 Must-Have Android Tools for Developers]]></title>
                     <description><![CDATA[<p><span id="internal-source-marker_0.7444858686067164">The Android SDK ships with  dozens of tools. These 10 are the most important for Android developers to  know.</span></p>]]></description>
                     <pubDate>Fri, 08 Apr 2011 15:54:00 -0700</pubDate>
                     <title><![CDATA[Define Factories in Spring XML Using Method Injection]]></title>
                     <description><![CDATA[<p>Use the power of ServiceLocatorFactoryBean to define conditional factories in Spring XML without any of the usual method injection limitations.</p>]]></description>
                     <pubDate>Mon, 04 Apr 2011 00:00:00 -0700</pubDate>
                     <title><![CDATA[Eclipse Pushing Orion Development with New Hub]]></title>
                     <description><![CDATA[<p>Eclipse effort to build a browser-based embeddable IDE continues to grow.</p>]]></description>
                     <pubDate>Tue, 22 Mar 2011 12:06:00 -0700</pubDate>
                     <title><![CDATA[Spring WS 2.0: New Integration Test Support]]></title>
                     <description><![CDATA[<p>Spring WS 2.0 introduces a new integration testing module, spring-ws-test, which makes writing integration tests for both client- and server-side code very easy.</p>]]></description>
                     <pubDate>Tue, 15 Mar 2011 00:00:00 -0700</pubDate>
                     <title><![CDATA[Android 3.0 Honeycomb SDK: The Good, the Bad and the Missing]]></title>
                     <description><![CDATA[<p>Android 3.0 (aka Honeycomb) is the largest update for Android in quite some time. Get a developer's first look at the new platform.</p>]]></description>
                     <pubDate>Wed, 09 Mar 2011 00:00:00 -0800</pubDate>
                     <title><![CDATA[10 Powerful PDT Tools, Tips and Tricks for PHP Developers]]></title>
                     <description><![CDATA[<p>If you're a PHP Developer Tools (PDT) user, these 10 tips will make you a much more efficient PHP developer on Eclipse.</p>]]></description>
                     <pubDate>Tue, 08 Mar 2011 00:00:00 -0800</pubDate>

Now that we have a response, we can go about doing something meaningful with it.


In this article, we saw how to use the WebClient class to make HTTP Requests on the Windows Phone platform. I hope you have found the information useful and can use it to make better Windows Phone applications.

This article was originally published on June 17th, 2011

About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Related Articles


Most Popular Programming Stories

More for Developers

RSS Feeds

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