Skip to content

Instantly share code, notes, and snippets.

@SlyNet
Created April 5, 2013 08:38
Show Gist options
  • Save SlyNet/5317631 to your computer and use it in GitHub Desktop.
Save SlyNet/5317631 to your computer and use it in GitHub Desktop.
Awaitable async file download with HttpWebRequest
public class ImageDownloader
{
public async Task<byte[]> Download(string url)
{
var request = (HttpWebRequest) HttpWebRequest.Create(url);
var response = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null);
using (var mStream = new MemoryStream())
{
response.GetResponseStream().CopyTo(mStream);
return mStream.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment