Wednesday, February 26, 2014

HttpClient Awesomeness


To many people `async` is old hat now...


But to many of us it's still quite new :)


But, **wow** is it awesome :)


Just like with Linq, to start with it doesn't feel that big a deal, but later - after you've been using it a bit - then you know that you are never going back :)


One of the first `async` components that people come across is `HttpClient`. Using this makes async network code really wonderfully writeable and readable :)


var client = new HttpClient();
var msg = await client.GetAsync(string.Format(MovieListUrl, ApiToken, searchTerm));
if (msg.IsSuccessStatusCode)
{
using (var stream = await msg.Content.ReadAsStreamAsync())
{
using (var streamReader = new StreamReader(stream))
{
var str = await streamReader.ReadToEndAsync();
var list = await JsonConvert.DeserializeObjectAsync<MovieList>(str);
return list;
}
}
}
view raw MovieList hosted with ❤ by GitHub
from https://github.com/Cheesebaron/RottenTomatoSample


Further, when you use `HttpClient` on iOS, it isn't just better looking code - it can also be better performing code - as it can make use of the native iOS HttpHandling instead of the Mono .Net one.


Last year, PaulBetts produced a github repo to assist with this http://log.paulbetts.org/fast-http-with-modernhttpclient/


To integrate that back into the PCL world of MvvmCross, a couple of regular MvvmCross contributors have posted lots of useful links:





This is awesome - and well deserving of badges #6 and #2 for Tomascz and Michael




AWESOMENESS!!!!

... although actually right now I seem to be missing a post... where is the first badge for @rid00z - will go hunt!

No comments:

Post a Comment