Provides classes for network programming. This namespace includes types for sending and receiving data over the network, handling network addresses, and managing network protocols.
Send an HTTP GET request:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://www.example.com");
response.EnsureSuccessStatusCode(); // Throw if HTTP status code is not 2xx
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody.Substring(0, 200) + "..."); // Display first 200 chars
}
catch (HttpRequestException e)
{
Console.WriteLine($"\nException Caught!");
Console.WriteLine($"Message :{e.Message} ");
}
}
}
}