Given a URL, what would be the most efficient code to download the contents of that web page? I am only considering the HTML, not associated images, JS and CSS.
+3
A:
From MSDN:
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main (string[] args)
{
if (args == null || args.Length == 0)
{
throw new ApplicationException ("Specify the URI of the resource to retrieve.");
}
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
}
}
Chris
2008-08-25 15:24:20
Good answer for later reference
Lea Cohen
2008-09-13 19:28:46
+8
A:
public static void DownloadFile(string remoteFilename, string localFilename)
{
WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
}
John Sheehan
2008-08-25 15:24:22
+7
A:
Use the WebClient class from System.Net; on .NET 2.0 and higher.
WebClient Client = new WebClient ();
Client.DownloadFile("http://mysite.com/myfile.txt", " C:\myfile.txt");
Adam Haile
2008-08-25 15:25:36
A:
Blah blah blah, your speed will be constrained by bandwidth, blah blah blah.
MSN
Mat Noguchi
2008-08-25 15:34:04