Saturday 16 January 2010

URL Shortening Service

A URL shortening service is not essential for the most part of the applications in the WEB and may be dangerous because the surfer doesn't see what lies behind the short URL. But if I would like to publish something on a short messaging system like Twitter I need a very short URL.

The solution would be to register a very short domain like goo.gl and to run my own shortener, but for my application the additional effort is out of scope. So I need to connect to an existing URL shortening service.

The beginner has a first obstacle: where to find an inexpensive reliable URL shortening service. Using a search engine I did found a lot of service which are also free of charge, but no way to proof if the are also reliable.

Service Requirements

Since I want to generate the short URL into the background I need a service with API support. Additionally stating from the short URL I want to obtain the original (long) URL.

The candidates

I didn't want to check all the services, for instance four of them are enough.

bit.ly

The service is free of charge, but  requires a registration and checks each request using an API key.

  • Homepage with a clear description of the service.
  • Simple well documented API.
  • Shorten and expand functions.
  • JSON and XML formatted response.
  • Simple but nice statistics summary.
  • Blog

I like bit.ly and is currently my first choice.

is.gd

The service is free of charge and doesn't require a registration.

  • Homepage with a clear description of the service.
  • Very simple API with documentation.
  • Shorten only function.
  • Plain text (URL only) response.

This service is very spartan; I noticed that after two or three days the short URL was different. If a very short URL is required id.gd may be a good choice. 

snipurl.com

The service is free of charge, but requires a registration and checks each request using an API key.

  • Homepage with a clear description of the service.
  • Simple well documented API (Firefox doesn't display the page and with the MSIE there are some curious side effects, try with is-there-a-snipr-snipurl-api).
  • Shorten and expand functions.
  • XML and plain text (URL only) formatted response.
  • Simple statistics but searchable statistics.
  • Help

SnipURL exists since the year 2000, which is a sign of reliability. I miss the JSON formatted response, but the service is good.

tinyurl.com

The service is free of charge and doesn't require a registration.

  • Homepage with a clear description of the service.
  • Very simple API.
  • Shorten only function.
  • Plain text (URL only) response.

The service is very spartan, but the short URL is the same over the time.

Test the services

I did test this services for a while using a very simple HTTP client.

Client Requirements

The client have to be essential, but should be able to:

  • retry the connection three times before to give up.
  • follow the redirects (max three levels of indirection).
  • support GET & POST.
  • support the secure socket layer (SSL, nice to have).

Implementation

I use a very simple implementation done in Java and based on the HttpURLConnection, the following snippet shows the essential part of the code:

...
URL url = URL("http://myserver.com");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.addRequestProperty("User-Agent","FastShortGood/1.0 Java/6");
...
// Extract the character set from the "Content-Type"
// header using a regular expression. If none available
// assume "UTF-8"
...
StringBuilder buf = new StringBuilder();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));
String line;
while (null != (line = br.readLine())) {
  buf.append(line).append("\n");
}
String body = buf.toString();

// Extract the short URL from response body using a
// regular expression.

For the complete implementation I need about 300 lines of code. The most of them handles exceptions and HTTP respose codes. The lightweight client works well with all of the shortener services.

No comments:

Post a Comment