.NET Library Reference

Namespace: System.Net

Provides classes for network programming. This namespace includes types for sending and receiving data over the network, handling network addresses, and managing network protocols.

Core Networking Classes

  • Sockets: Low-level network communication.
  • Http: Classes for sending and receiving data using HTTP.
  • WebClient: A simple client class for sending data to and receiving data from a resource identified by a URI.
  • IPAddress: Represents an Internet Protocol (IP) address.
  • IPEndPoint: Represents a network endpoint as an IP address and a port number.

Protocols and Services

  • DNS: Resolves host names to IP addresses.
  • TcpClient: Provides simple client connections for the TCP protocol.
  • UdpClient: Provides simple client connections for the UDP protocol.
  • WebHeaderCollection: Represents the collection of HTTP header names and values.
  • NetworkInformation: Network interface and statistics.

Security and Authentication

  • Security: Classes for network security.
  • Cache: Classes for managing network resource caching.
  • HttpClient: A modern API for sending HTTP requests.

Common Usage Example (HttpClient)

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} ");
            }
        }
    }
}