Jim Lawless' Blog


Expanding Shortened URL's

Originally published on: Wed, 01 Jul 2009 01:38:18 +0000

Lately, I've read some complaints about URL shorteners ... sites that encode a URL into a shorter URL. The shortener web site keeps a record of the original URL and the smaller code that it has provided. All who navigate to the short URL are redirected to the original URL.

Unfortunately, as handy as these shorteners are, it's difficult to determine where one's browser might end up when clicking a link.

I wrote a small command-line C# program to accept a URL, retrieve the document, and display the final endpoint address. If the original URL had been redirected in any way, this displayed address would likely be different than the input address.

urlx.cs


// urlx.cs - Simple command-line utility to expand shortened URL's.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


using System ;
using System.IO ;
using System.Net;

namespace URLX
{
   public class URLX
   {
      public static void Main(string[] args)
      {
         if (args.Length == 1)
         {
            try
            {
               // Create a request to the passed URI.
               HttpWebRequest req =
                  (HttpWebRequest)WebRequest.Create (args[0]);
               req.AllowAutoRedirect=true;
               HttpWebResponse res =
                  (HttpWebResponse)req.GetResponse ();

               ServicePoint sp = req.ServicePoint;
               Console.WriteLine("End address is " + sp.Address.ToString());
            }
            catch(Exception e)
            {
               Console.WriteLine(e.Source);
               Console.WriteLine(e.Message);
            }
         }
         else
         {
            Console.WriteLine("Usage: urlx.exe url");
         }
      }
   }
}

As an example, the URL:


http://www.mailsend-online.com/blog?p=9

...is a link to my writeup on generating transparent, single-pixel GIF's for use in tracking the use of one's web site. By using the shortener service at http://bit.ly , and encoded redirector for my article URL becomes http://bit.ly/Rmncf .

To use my new urlx utility, I enter the following on the command-line:

urlx http://bit.ly/Rmncf

I see the output:


   End address is http://www.mailsend-online.com/blog?p=9

NOTE: I've added a GUI to urlx in a separate article. Please see http://www.mailsend-online.com/blog/?p=20

You may download the source and .NET 2.0 EXE for urlx here:

http://www.mailsend-online.com/wp/urlx.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Generating Primes with XSLT
Next post:A GUI for urlx


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

The October 10th Bug

Thwarting HTTP Referer Trackbacks

Screen Capture from Multiple Monitors in Java

A Data Manipulation Library for TAP

Scott Ballantyne: Blazin' Into Forth

Learning Z-80 Assembly Language on the TRS-80

E-mail cleansing

Backlinks to my Blog Posts

Obfuscated Perl

My Journey Began with BASIC


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service