Created
April 5, 2013 08:38
-
-
Save SlyNet/5317631 to your computer and use it in GitHub Desktop.
Awaitable async file download with HttpWebRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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