System.Net Namespace

The System.Net namespace provides access to the Windows networking classes that enable you to request or receive data from network resources. It provides a data-driven, remote data access solution.

Key Classes

This namespace contains classes for network programming, including:

Class Name Description
IPAddress Represents an Internet Protocol (IP) address.
IPEndPoint Represents a network endpoint as an IP address and a port number.
Uri Represents a Uniform Resource Identifier (URI) reference.
WebService Provides functionality for creating Web services.
NetworkCredential Represents the credentials for basic, digest, or NTLM authentication.
CookieContainer Stores and manages collections of cookies associated with a URI.
HttpListener Provides a simple, programmable HTTP protocol listener.

IPAddress Class

The IPAddress class represents an Internet Protocol (IP) address. It can represent an IPv4 address or an IPv6 address. This class is fundamental for establishing network connections.

using System.Net;

// Create an IPv4 address
IPAddress ipv4Address = IPAddress.Parse("192.168.1.1");

// Create an IPv6 address
IPAddress ipv6Address = IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334");

// Get the loopback address
IPAddress loopback = IPAddress.Loopback;

// Get the broadcast address
IPAddress broadcast = IPAddress.Broadcast;

IPEndPoint Class

The IPEndPoint class represents a network endpoint as an IP address and a port number. It is used to specify a destination for network communications.

using System.Net;

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
int port = 8080;

IPEndPoint endPoint = new IPEndPoint(ipAddress, port);

Uri Class

The Uri class represents a Uniform Resource Identifier (URI) reference. URIs are used to identify resources on the internet or on a local network.

using System;

Uri websiteUri = new Uri("https://www.example.com/path/to/resource");
Uri relativeUri = new Uri("/another/resource", UriKind.Relative);
Uri combinedUri = new Uri(websiteUri, relativeUri);

Console.WriteLine(combinedUri.AbsoluteUri);

WebService Class

Provides functionality for creating Web services. Typically, you would use classes from System.Web.Services or System.ServiceModel for modern web service development.

NetworkCredential Class

The NetworkCredential class is used to store and transmit authentication credentials, such as a username, password, and domain, to a network resource.

using System.Net;

NetworkCredential credentials = new NetworkCredential("username", "password", "domain");

CookieContainer Class

The CookieContainer class manages a collection of cookies for a particular URI. It helps in maintaining session state across HTTP requests.

using System.Net;

CookieContainer cookieJar = new CookieContainer();
Uri uri = new Uri("https://www.example.com");
cookieJar.Add(uri, new Cookie("sessionID", "12345abc"));

HttpListener Class

The HttpListener class allows an application to listen for incoming HTTP requests. It can be used to build simple HTTP servers without requiring a full web server.

using System.Net;
using System.Text;

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8080/");
listener.Start();
Console.WriteLine("Listening on http://localhost:8080/");

HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;

string responseString = "

Hello, World!

"; byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); response.OutputStream.Close(); listener.Stop();

For more detailed information on specific classes and methods, please refer to the relevant API documentation.