15 January 2013

Opening URL for request

A collegue required me to run a sheduled task on a our company IIS webserver to
open an URL which sends emails to our customers.

I googled and most solutions where created using MSDOS batch or VBScript.

The problem is that it is harder to control the page (close) after the request has been done and the page will show on desktop...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace openurl
{
    class Program
    {
        static void Main(string[] args)
        {

            foreach (string s in args)
            {

                Match m = Regex.Match(s, @"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$");

                if (m.Success)
                {

                    try
                    {

                    Console.WriteLine("Opening " + s);

                    WebRequest rq = WebRequest.Create(s);
                    WebResponse rs = rq.GetResponse();

                    rq = null;
                    rs = null;

                    }
                    catch (Exception ex)
                    {

                        Console.WriteLine(ex.Message);
                        Console.ReadLine();

                    }

                }
                else
                {
                    Console.WriteLine(s + "is not a valid URL");
                    Console.ReadLine();
                }

            }

        }
    }
}
+++ download project +++

* credit for regex goes to net.tutsplus.com

No comments:

Post a Comment