Last active
May 18, 2016 00:08
-
-
Save LGM-AdrianHum/0953a80867c9fe1a6a59e92e7af6e3d2 to your computer and use it in GitHub Desktop.
DownloadAsync for HTTP
This file contains 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 static class WebUtils | |
{ | |
private static Lazy<IWebProxy> proxy | |
= new Lazy<IWebProxy>(() => string.IsNullOrEmpty(Settings.Default.WebProxyAddress) ? null : | |
new WebProxy { Address = new Uri(Settings.Default.WebProxyAddress), UseDefaultCredentials = true }); | |
public static IWebProxy Proxy | |
{ | |
get { return WebUtils.proxy.Value; } | |
} | |
public static Task DownloadAsync(string requestUri, string filename) | |
{ | |
if (requestUri == null) | |
throw new ArgumentNullException("requestUri"); | |
return DownloadAsync(new Uri(requestUri), filename); | |
} | |
public static async Task DownloadAsync(Uri requestUri, string filename) | |
{ | |
if (filename == null) | |
throw new ArgumentNullException("filename"); | |
if (Proxy != null) | |
{ | |
WebRequest.DefaultWebProxy = Proxy; | |
} | |
using (var httpClient = new HttpClient()) | |
{ | |
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri)) | |
{ | |
using ( | |
Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(), | |
stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, Constants.LargeBufferSize, true)) | |
{ | |
await contentStream.CopyToAsync(stream); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment