System.Net.WebClient
Provides a simple, programmable interface to transmit data using the HTTP, FTP, and file protocols. This class is useful for applications that need to send and receive data from a resource over a network.
Properties
BaseAddress
string BaseAddress { get; set; }
Gets or sets the base address of URIs used in requests made by this WebClient. This property can be used to simplify requests to a specific server.
Credentials
ICredentials Credentials { get; set; }
Gets or sets the credentials used to authenticate the request to the server. This is useful for accessing resources that require authentication.
Headers
WebHeaderCollection Headers { get; }
Gets the collection of HTTP headers associated with the request. You can use this property to add custom headers or modify existing ones.
Methods
DownloadString
string DownloadString(string address)
Downloads the resource with the specified URI to a string.
Parameters
- address: The URI of the resource to download.
Returns
- A string containing the resource.
UploadString
string UploadString(string address, string data)
Uploads the specified string to the specified resource.
Parameters
- address: The URI of the resource to upload to.
- data: The string to upload.
Returns
- A string containing the response from the server.
DownloadFile
void DownloadFile(string address, string fileName)
Downloads the resource with the specified URI to a local file.
Parameters
- address: The URI of the resource to download.
- fileName: The name of the local file to store the downloaded content.
UploadFile
byte[] UploadFile(string address, string method, string fileName)
Uploads a local file to a resource specified by a URI.
Parameters
- address: The URI of the resource to upload to.
- method: The HTTP method to use for the upload (e.g., "POST").
- fileName: The path to the local file to upload.
Returns
- A byte array containing the response from the server.
Events
DownloadProgressChanged
event DownloadProgressChangedEventHandler DownloadProgressChanged
Occurs when progress is made during an asynchronous download operation.
UploadProgressChanged
event UploadProgressChangedEventHandler UploadProgressChanged
Occurs when progress is made during an asynchronous upload operation.
C# Example: Downloading a Web Page
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";
Console.WriteLine($"Downloading content from: {url}");
string html = client.DownloadString(url);
Console.WriteLine("Download complete. Content length: " + html.Length);
// You can further process the 'html' string here.
// For brevity, we won't print the entire HTML.
Console.WriteLine("First 100 characters: " + html.Substring(0, Math.Min(100, html.Length)));
}
catch (WebException e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
}
C# Example: Uploading a File
using System;
using System.Net;
using System.IO;
public class FileUploader
{
public static void Main(string[] args)
{
string url = "https://httpbin.org/post"; // A service that echoes POST data
string filePath = "my_document.txt";
string contentToUpload = "This is the content of my file.";
// Create a dummy file for demonstration
File.WriteAllText(filePath, contentToUpload);
Console.WriteLine($"Created temporary file: {filePath}");
using (WebClient client = new WebClient())
{
try
{
Console.WriteLine($"Uploading {filePath} to {url}");
// The method "POST" is common for uploads.
byte[] responseBytes = client.UploadFile(url, "POST", filePath);
string responseString = System.Text.Encoding.UTF8.GetString(responseBytes);
Console.WriteLine("Upload successful. Server response:");
Console.WriteLine(responseString);
}
catch (WebException e)
{
Console.WriteLine($"An error occurred during upload: {e.Message}");
}
finally
{
// Clean up the dummy file
if (File.Exists(filePath))
{
File.Delete(filePath);
Console.WriteLine($"Deleted temporary file: {filePath}");
}
}
}
}
}