Windows Phone 7 WebHelper

By on 9/9/2010

Just a quick post, this is a nice little helper class I wrote to simplify doing HTTP get requests in windows phone 7 applications. Not that it's very difficult, but this hides some of the unnecessary details involved in setting up the asynchronous web request and gives you a super simple API that is easy to understand.

public static class WebHelper
{
    public static void Get(string url, Action<string> action)
    {
        Get(new Uri(url), action);
    }

public static void Get(Uri uri, Action<string> action) { var request = WebRequest.CreateHttp(uri);

request.BeginGetResponse(i => { var response = request.EndGetResponse(i); var sreader = new StreamReader(response.GetResponseStream()); var result = sreader.ReadToEnd(); action(result); }, null); } }
Using this class could not be easier:

string u = @"http://bing.com";
WebHelper.Get(u, html =>
    {
        Debug.WriteLine(html);
    });


This class can (and will) be expanded for things like posting form values ... I'll update this post if I do any of those updates :-)

See more in the archives