You want to use the WebClient class in the System.Net namespace to download web pages and files using the C# language targeting the .NET Framework. This class makes it possible to easily download web pages for testing, allowing you to automate important tests for important web sites. Here we look at the WebClient class.
Key point:Use WebClient to download files on the Internet.
Example 1
First, to use the WebClient class in your C# code you need to either use the fully specified name System.Net.WebClient or include the System.Net namespace with a using directive. This example uses the namespace and creates a new WebClient object instance and sets its user agent as Internet Explorer 6. This WebClient will then download a page and the server will think it is Internet Explorer 6, allowing you to test this case.
Program that uses client user-agent [C#]
using System;
using System.Net;
class Program
{
static void Main()
{
// Create web client simulating IE6.
using (WebClient client = new WebClient())
{
client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" +
" (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
// Download data.
byte[] arr = client.DownloadData("http://www.dotnetperls.com/");
// Write values.
Console.WriteLine("--- WebClient result ---");
Console.WriteLine(arr.Length);
}
}
}
Output
--- WebClient result ---
6585
Simulating user-agent header. You can add a new HTTP header to your WebClients download request by assigning an entry in the Headers collection. You can also use the WebHeaderCollection returned by Headers and call the Add, Remove, Set and Count methods on it.
Getting byte arrays. The DownloadData instance method on the WebClient is called and its reference return value is assigned to a new byte array reference. Internally, the DownloadData method will allocate the bytes on the managed heap. When you assign the result to the variable, you are doing a bitwise copy of the reference to that data.
Disposing of WebClient. The program shows that you can use the 'using' statement to ensure that the system resources for the WebClient are cleaned up by the system by placing them on the finalization queue. This is critical for longer programs but not needed for very short and trivial programs.