UrlFetch Command Line Utility

We got some questions recently about how you could run a web page from Task Manager without script.

We got some questions recently about how you could run a web page from Task Manager without script.


We got some questions recently about how you could run a web page from Task Manager without script. Well, one answer is to run an executable, so here's a convenient console app utility that accepts two command - line arguments:
1) the url of the web page to request
2) an optional file name and path to save the web page contents (useful for reports, etc.

Now here's the code (man, this ain't rocket science):

using System;
using System.Net;
using System.IO;
namespace UrlFetch
{
class UrlFetcher
{
[STAThread]
static void Main(string[] args)
{
if(args.Length ==0)
{
Console.WriteLine("Usage: UrlFetch <http://mysite.com/mypage.htm>; <SaveFileName>");
Console.WriteLine("SaveFileName is optional.");
System.Environment.Exit(0);
}
WebClient w= new WebClient();
byte[] b=w.DownloadData (args[0]);
if(args.Length>0)
{
FileStream strm =
new FileStream(System.Environment.CurrentDirectory+@"\"+args[1],FileMode.CreateNew);
strm.Write(b,0,b.Length);
}
System.Environment.Exit(0);
}
}
}

NOTE: When adding this as a task in Scheduled Tasks Control Panel interface, you may not be able to have more than one querystring item because the Ampersand is considered an invalid character on the command line. The fix is to use a seldom-used symbol such as the pipe ( | ) as an alternative, and parse it out in the page processing routine to split up the querystring items.

By Peter Bromberg   Popularity  (482 Views)