using System.Net; using System.Threading.Tasks; public static async TaskGetResponse(string url) { HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { return await reader.ReadToEndAsync(); } } }
using System.Net; using System.Threading.Tasks; public static async Task DownloadFile(string url, string path) { HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync()) { using (Stream stream = response.GetResponseStream()) { using (FileStream fileStream = new FileStream(path, FileMode.CreateNew)) { await stream.CopyToAsync(fileStream); } } } }This code example shows how to use the GetResponseAsync method to download a file from a specified URL and save it to a local directory. Package library: System.Net.Http or System.Net.HttpWebRequest