Screen Grabbing with .NET
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]");
}
string url = args[0];
string path = Path.GetFullPath(args[1]);
string contents = string.Empty;
if (File.Exists(path))
{
Console.WriteLine(" -> Deleting file...");
File.Delete(path);
}
Console.WriteLine(" -> Fetching web page...");
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
Console.WriteLine(" -> Status Code: " + response.StatusCode);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new ArgumentException("Url returned a bad status code: " +
response.StatusCode.ToString());
}
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
contents = reader.ReadToEnd();
}
}
contents = contents.Trim();
if (contents.Length < 1)
{
throw new Exception("The web page has no content!");
}
Console.WriteLine(" -> Writing file...");
using (StreamWriter writer = File.CreateText(path))
{
writer.Write(contents);
}
Console.WriteLine(" -> All done!");
}
catch (Exception ex)
{
Console.WriteLine("Whoops! Something went wrong.");
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine(Environment.NewLine + "Press enter to exit.");
Console.ReadLine();
}
}
}
}