Monthly Archives: March 2013

Async and Await for HTTP Networking, Part 2–HttpClient

In my previous post on this topic, Async and Await for Http Networking on Windows Phone, I described how you can use extension methods to create awaitable methods on the WebClient and HttpWebRequest APIs. Courtesy of Microsoft’s .NET Framework team, there have been more developments in this area – the creation of a portable implementation of the HttpClient API, until now available only for Windows 8 Store and .NET Framework 4.5 apps. This API will allow developers to consume HttpClient on Windows Phone 7.5 and higher, Windows Store apps, and .NET Framework 4.0 and higher.

Portable HttpClient for .NET Framework and Windows Phone

As they describe on their blog post here, the .NET Framework team have released a Beta release of the portable version of HttpClient, the modern networking API. The HttpClient library is a set of APIs for .NET which provide a flexible and extensible way to access all things exposed through HTTP. HttpClient exposes the new Task based asynchronous methods, which makes writing responsive and performant UI applications across all platforms a lot simpler.

This is particularly significant for anyone developing apps for both Windows Phone and Windows 8 as it allows you to standardise your networking code around HttpClient.  In addition it also enables other portable library developers who require networking support to use HttpClient while targeting all or a subset of the supported platforms.

To use it, first make sure you have got NuGet v2.1 or later installed (Go to Tools – Extensions and Updates). Then to use the HttpClient package, right click on your solution, go to the Manage Nuget Packages dialog, search for Id Microsoft.Net.Http, and make sure “Include Prerelease” is turned on.

Now you can go ahead and program against the HttpClient API. For example, here’s the code to access the OData feed for the Northwind sample feed at

HttpClient httpClient = new HttpClient();    
httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
HttpResponseMessage response = null;
    response = await httpClient.GetAsync(

    response.EnsureSuccessStatusCode(); // Throws exception if bad HTTP status code
    Debug.WriteLine(response.StatusCode + " " + response.ReasonPhrase);

    string responseBodyAsText = await response.Content.ReadAsStringAsync();
    var feed = Newtonsoft.Json.JsonConvert.DeserializeObject<SupplierODataFeed>(responseBodyAsText);

    MessageBox.Show("Received payload of " + responseBodyAsText.Length + " characters");

    SuppliersList.ItemsSource = feed.d.results;
catch (HttpRequestException hre)
    MessageBox.Show("RespCallback Exception raised! Message:{0}" + hre.Message);

Using Compression with the Portable HttpClient

Anyone who knows me or has seen the Networking JumpStart video, will know that I am running a bit of  a crusade against the non-application of compression. Too many devs forget the simple fact that mobile apps on cellular networks (that’s not just Phone people – same for Windows 8 machines that have integrated cellular networking!) often find themselves trying to communicate over poor quality networks. Stack the cards in your favour, increase the chance of success and USE COMPRESSION!

Fortunately, many other people share my views on this. The excellent people at AdvancedREI have created the HttpClient.Compression NuGet package, based on original work by the super-excellent Morten Nielsen.

This is also a pre-release NuGet package. After you install that package, all you do is new up an instance of the CompressedHttpClientHandler and pass that into the constructor of HttpClient:

// Requires HttpClient.Compression NuGet package
var handler = new AdvancedREI.Net.Http.Compression.CompressedHttpClientHandler(); 

HttpClient httpClient = new HttpClient(handler); 

As long as your server is configured to serve up compressed content, you will receive data compressed over the wire and the handler will take care of decompression on receipt.

Download the sample code here

UK App Builders: Reward Program *One Day Special* and Bonus Rewards

Show us that you’re a game changer and collect points every step of the way to redeem rewards like a brand new Windows 8 RT Slate. Create the next app phenomenon by building your original and unique Windows Store and/or Windows Phone App.

Get recognised and rewarded for building your original apps. Further, get bonus rewards for migrating your Windows Phone app to Windows Store and vice versa or incorporating one or more Windows Azure Services.

Follow these 4 Simple Steps to Start Earning Points for your Awesome Apps:

1. Register yourself on the UK App Builder Reward Programme website.

2. Build & publish your original & unique app.

3. Claim points by telling us about the app you’ve published and we’ll award points to you accordingly.

4. Redeem your points for a range of fantastic prizes and gadgets.

Introducing Our First Ever One Day Special

On Tuesday 19 March 2013, we will be announcing a special one day deal for the Rewards Programme. To make sure you can take advantage of this special one day deal, publish your apps and ensure you claim your points before Thursday 14 March 2013 (less than 2 weeks away) so you will have your all of your eligible points in your account to use. You do not want to miss this one day special! Look out for the announcement for the One Day Special on Tuesday 19 March.

Don’t forget about our Bonus Rewards

The bonus rewards are limited deals where you must fulfil the stated criteria. Once you have fulfilled the criteria, you will be notified by our team and delivery of your bonus reward will be arranged. The bonus reward is exactly that and it will not affect the balance of your points therefore, you will still be able to use your points to claim for your reward(s) of your choice. Nokia have generously sponsored the first set of bonus rewards. You have to be quick to take advantage of this special bonus reward.

Bonus Reward 1: Receive a Nokia Lumia 620 Windows Phone 8 Handset if you are one of the first 20 App Builders who accumulate 100 points from Windows Phone Apps published.

Bonus Reward 2: Receive a Nokia Lumia 920 Windows Phone 8 Developer Handset if you are one of the first 10 App Builders who accumulate 200 points for publishing Windows Phone apps.