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

Introduction

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
);
           webClientInstance.OpenReadAsync(serviceUri);
 
        }

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">
<channel>
<title><![CDATA[Developer.com Open Source]]></title>
<link>http://www.developer.com</link>
<description><![CDATA[Developer.com Open Source]]></description>
<language>en-us</language>
<copyright>Copyright 1996-2011 Quin Street Corporation</copyright>
<managingEditor>rss@internet.com</managingEditor>
<webMaster>rss@internet.com</webMaster>
<docs>http://backend.userland.com/rss</docs>
              <item>
                     <title><![CDATA[Oracle Commits to Open Source Hudson at Eclipse]]></title>
                     <link>http://www.developer.com/open/oracle-commits-to-open-source-hudson-at-eclipse.html</link>
                     <guid>http://www.developer.com/open/oracle-commits-to-open-source-hudson-at-eclipse.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Oracle Advances NetBeans for Java 7]]></title>
                     <link>http://www.developer.com/java/oracle-advances-netbeans-for-java-7.html</link>
                     <guid>http://www.developer.com/java/oracle-advances-netbeans-for-java-7.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Using the New Python 3.2 Concurrent Programming Features]]></title>
                     <link>http://www.developer.com/lang/other/using-the-new-python-32-concurrent-programming-features.html</link>
                     <guid>http://www.developer.com/lang/other/using-the-new-python-32-concurrent-programming-features.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Adobe Extends Flash Builder for Mobile and PHP]]></title>
                     <link>http://www.developer.com/open/adobe-extends-flash-builder-for-mobile-and-php.html</link>
                     <guid>http://www.developer.com/open/adobe-extends-flash-builder-for-mobile-and-php.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[10 Must-Have Android Tools for Developers]]></title>
                     <link>http://www.developer.com/ws/10-must-have-android-tools-for-developers.html</link>
                     <guid>http://www.developer.com/ws/10-must-have-android-tools-for-developers.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Define Factories in Spring XML Using Method Injection]]></title>
                     <link>http://www.developer.com/java/ent/define-factories-in-spring-xml-using-method-injection.html</link>
                     <guid>http://www.developer.com/java/ent/define-factories-in-spring-xml-using-method-injection.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Eclipse Pushing Orion Development with New Hub]]></title>
                     <link>http://www.developer.com/open/eclipse-pushing-orion-development-with-new-hub.html</link>
                     <guid>http://www.developer.com/open/eclipse-pushing-orion-development-with-new-hub.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Spring WS 2.0: New Integration Test Support]]></title>
                     <link>http://www.developer.com/java/ent/spring-ws-2.0-new-integration-test-support.html</link>
                     <guid>http://www.developer.com/java/ent/spring-ws-2.0-new-integration-test-support.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[Android 3.0 Honeycomb SDK: The Good, the Bad and the Missing]]></title>
                     <link>http://www.developer.com/ws/android-3.0-honeycomb-sdk-the-good-the-bad-and-the-missing.html</link>
                     <guid>http://www.developer.com/ws/android-3.0-honeycomb-sdk-the-good-the-bad-and-the-missing.html</guid>
                     <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>
              </item>
                     <item>
                     <title><![CDATA[10 Powerful PDT Tools, Tips and Tricks for PHP Developers]]></title>
                     <link>http://www.developer.com/lang/php/10-powerful-pdt-tools-tips-and-tricks-for-php-developers.html</link>
                     <guid>http://www.developer.com/lang/php/10-powerful-pdt-tools-tips-and-tricks-for-php-developers.html</guid>
                     <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>
              </item>
       </channel>
</rss>
----- -END OF HTTP WebClient RESPONSE

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

Summary

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.



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

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds