| Class | Description |
|---|---|
| WebClient | Provides an easy-to-use .NET object for sending data to and receiving data from a resource identified by a URI. |
| UploadValuesCompletedEventArgs | Provides data for the UploadValuesCompleted event. |
| DownloadFileCompletedEventArgs | Provides data for the DownloadFileCompleted event. |
| DownloadStringCompletedEventArgs | Provides data for the DownloadStringCompleted event. |
| OpenReadCompletedEventArgs | Provides data for the OpenReadCompleted event. |
| OpenWriteCompletedEventArgs | Provides data for the OpenWriteCompleted event. |
| WebClientAsyncResult | Represents an asynchronous operation. |
The System.Net.WebClient namespace contains classes that simplify network operations. The primary class, WebClient, allows you to easily download data from a URI, upload data to a URI, and send requests to web services. It supports various protocols like HTTP, FTP, and file (local file access).
Here's a simple example of how to download the content of a web page using WebClient:
using System;
using System.Net;
public class Example
{
public static void Main(string[] args)
{
using (WebClient client = new WebClient())
{
try
{
string url = "https://www.example.com";
string html = client.DownloadString(url);
Console.WriteLine($"Successfully downloaded content from {url}:");
Console.WriteLine(html.Substring(0, Math.Min(html.Length, 500)) + "..."); // Display first 500 characters
}
catch (WebException e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
}
This example demonstrates downloading a string from a given URL. For more complex scenarios, such as uploading files or handling asynchronous operations, refer to the specific class documentation.