Windows Store (WinRT/Metro)–Network Helper Methods


In getting used to WinRT and the changes in the Framework, we need to find new ways to do things that we used to be able to do one way or another. I will update this as I find better ways of doing things or add more code, but here is my first pass at a few methods to facilitate downloading content from the Internet as a string, stream or byte array (for downloading pages, web service content, images, files, etc). The code was written in VB.Net. None of the converters that I use have been updated to handle some of the Async code from 4.5.

VB.Net

    Imports System.Net.Http
    
    Namespace Net
    
        Public Class NetHelpers        
            '*********************************************************************************************************************
            '
            '             Class:  NetHelpers
            '      Initial Date:  08/31/2012
            '      Last Updated:  08/31/2012
            '     File Revision:  0
            '     Programmer(s):  Blake Pell (blakepell@hotmail.com, http://www.blakepell.com)
            '
            '*********************************************************************************************************************
    
            ''' <summary>
            ''' Downloads a specified url it's content as a String.
            ''' </summary>
            ''' <param name="url"></param>
            Public Async Function DownloadString(url As String) As Task(Of String)
                Dim handler As New HttpClientHandler
                handler.UseDefaultCredentials = True
                handler.AllowAutoRedirect = True
                Dim client As New HttpClient(handler)
                Dim response As HttpResponseMessage = Await client.GetAsync(url)
                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsStringAsync
            End Function
    
            ''' <summary>
            ''' Downloads a specified url and downloads it's content as a Byte array.
            ''' </summary>
            ''' <param name="url"></param>
            Public Async Function DownloadByteArray(url As String) As Task(Of Byte())
                Dim handler As New HttpClientHandler
                handler.UseDefaultCredentials = True
                handler.AllowAutoRedirect = True
                Dim client As New HttpClient(handler)
                Dim response As HttpResponseMessage = Await client.GetAsync(url)
                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsByteArrayAsync
            End Function
    
            ''' <summary>
            ''' Downloads a specified url it's content as a System.IO.Stream.
            ''' </summary>
            ''' <param name="url"></param>
            Public Async Function DownloadStream(url As String) As Task(Of System.IO.Stream)
                Dim handler As New HttpClientHandler
                handler.UseDefaultCredentials = True
                handler.AllowAutoRedirect = True
                Dim client As New HttpClient(handler)
                Dim response As HttpResponseMessage = Await client.GetAsync(url)
                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsStreamAsync
            End Function
    
        End Class
    
    End Namespace