Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
The following using declaration used in the sample code in the HttpClient Class is introduced in C# version 8.0:
using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
Above declaration is not available in the project of .NET Framework 4.7.2 where its C# version is 7.3.
Use previous expression as follow:
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleAppNet472
{
internal class Program
{
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
using (HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/"))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
}