Yesterday was hellishly long, leading to a drought in creative thought, which means that now it's time for a mini how-to on .NET programming.

using System.Net;

.NET makes it ridiculously easy to grab content from web pages. We're specifically interested in two classes in the System.Net namespace --- HttpWebRequest and HttpWebResponse (their respective parents, WebRequest and WebResponse can also be used).

I created a tiny little console application that takes a url and outputs the web page's content to the specified file on the hard drive.

Code's below the fold; it's pretty self-explanatory, so I'll leave you to it.

using System;    // For Console
using System.IO;   // For Path, StreamReader, StreamWriter
using System.Net;   // For HttpWebRequest, HttpWebResponse

namespace ScreenGrabber { public class Program { public static void Main(string[] args) { try { if (args.Length != 2) { throw new ArgumentException("Usage: screengrabber [url] [filepath]"); }

<span class="kwd">string</span> url = args[0];

<span class="kwd">string</span> path = Path.GetFullPath(args[1]);

<span class="kwd">string</span> contents = <span class="kwd">string</span>.Empty;

<span class="kwd">if</span> (File.Exists(path))
{
 Console.WriteLine(<span class="str">" -> Deleting file..."</span>);

 File.Delete(path);
}

Console.WriteLine(<span class="str">" -> Fetching web page..."</span>);

HttpWebRequest request = HttpWebRequest.Create(url) <span class="kwd">as</span> HttpWebRequest;

<span class="kwd">using</span> (HttpWebResponse response = request.GetResponse() <span class="kwd">as</span> HttpWebResponse)
{
 Console.WriteLine(<span class="str">" -> Status Code: "</span> + response.StatusCode);

 <span class="kwd">if</span> (response.StatusCode != System.Net.HttpStatusCode.OK)
 {
  throw <span class="kwd">new</span> ArgumentException(<span class="str">"Url returned a bad status code: "</span> + 
   response.StatusCode.ToString());
 }

 <span class="kwd">using</span> (StreamReader reader = <span class="kwd">new</span> StreamReader(response.GetResponseStream()))
 {
  contents = reader.ReadToEnd();
 }
}

contents = contents.Trim();

<span class="kwd">if</span> (contents.Length < 1)
{
 throw <span class="kwd">new</span> Exception(<span class="str">"The web page has no content!"</span>);
}

Console.WriteLine(<span class="str">" -> Writing file..."</span>);

<span class="kwd">using</span> (StreamWriter writer = File.CreateText(path))
{
 writer.Write(contents);
}

Console.WriteLine(<span class="str">" -> All done!"</span>);

}

catch (Exception ex) { Console.WriteLine("Whoops! Something went wrong.");

Console.WriteLine(<span class="str">"Message: "</span> + ex.Message);

Console.WriteLine(Environment.NewLine + "Press enter to exit."</span>);

Console.ReadLine();

} } } }