public GetStringAsync ( |
||
requestUri | ||
return | Task |
using System; using System.Net.Http; using System.Threading.Tasks; public class HttpClientExample { static readonly HttpClient client = new HttpClient(); public static async Task Main() { try { HttpResponseMessage response = await client.GetAsync("https://www.example.com/"); response.EnsureSuccessStatusCode(); // Throw if not a success code. string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } catch(HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ",e.Message); } } }
using System; using System.Net.Http; using System.Threading.Tasks; public class HttpClientExample { static readonly HttpClient client = new HttpClient(); public static async Task Main() { try { HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1"); response.EnsureSuccessStatusCode(); // Throw if not a success code. string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } catch(HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ",e.Message); } } }In this example, we use the GetStringAsync method to retrieve the response from an HTTP GET request to the specified URL which returns a JSON object. The response is then read as a string using ReadAsStringAsync() and printed to the console. Package library: System.Net.Http