public OpenReadTaskAsync ( System address ) : System.Threading.Tasks.Task |
||
address | System | |
return | System.Threading.Tasks.Task |
using System; using System.Net; using System.Threading.Tasks; public class MyWebClient { public async TaskDownloadDataAsync(string url) { using (var client = new WebClient()) { var stream = await client.OpenReadTaskAsync(url); var buffer = new byte[32768]; using (var ms = new MemoryStream()) { int read; while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } } }
using System; using System.Net; using System.Threading.Tasks; public class MyWebClient { public async TaskThis code downloads the contents of a URL as a string asynchronously using the OpenReadTaskAsync method. It utilizes a StreamReader to read the downloaded data and returns the resulting string. The package library for System.Net.WebClient is part of the .NET Framework.DownloadStringAsync(string url) { using (var client = new WebClient()) { var stream = await client.OpenReadTaskAsync(url); using (var sr = new StreamReader(stream)) { return sr.ReadToEnd(); } } } }
public OpenReadTaskAsync ( System address ) : System.Threading.Tasks.Task |
||
address | System | |
return | System.Threading.Tasks.Task |