Small is Beautiful: working with short URLs

Today’s world limits our expressions lenght to 140 character. No matter you text SMS or Tweet, you have to fit your sentences based on this restriction. Further to that, nobody has toleration for longer statements. That’s why, text material should be picked and chosen. Herein, some subsdiary materials such as long links would cause to go waste. To sum up, small is beautiful.

Bitly’s blowfish logo stands for url shortening

URL shortening services redirect long urls to shorter one. This kind of urls are friendly for messaging technologies requiring limited number of characters such as sms. Thus, you can append a link into your message and message quota would not be exceeded. Moreover, short urls are memorable. That’s why, referencig short urls are common in hard copied paragraphs such as newspapers or magazines.

Formely, Twitter automatically shortens links with Bitly service, this makes Bitly popular. Today, The Blue Bird consumes their own service.

What’s more, Bitly and Google are the most common url shortening service providers for end users. Although, web inteface of these service providers are easy to consume, urls should be shorten manuelly. In this post, we will focus on how to consume these services in our programs.

Bitly is dedicated firm for url shortening whereas url shortening is one of the services of Google. This means that Google might terminate url shortening service if the service would not be profitable. Similarly, Google terminates Freebase or Google Reader in the past. On the other hand, Bitly makes money only on url shortening. That’s why, termination of Bitly’s service would be surprise. Anyhow, we will mention integrating for both Bitly and Google.

No matter which service provider you consume, you need to register you projet and create a key. You should apply following orders to create application keys.

If you prefer to consume Google as url shortening service, you need to visit Google Developer Console first. Then, you should visit the following menu path to create your custom key: URL Shortener API > Credentials > Create Credentials > API key.

Otherwise, you should visit Bitly Developer Console to consume Bitly’s service. Then you can create your project and key on main page.

The following java code is developed to shorten urls. It consumes both Bitly and Google services. The both services are consumed as REST services. The main difference between accessing Bitly and Google is http methods. Bitly provides to access its service with HTTP GET method whereas Google provides to access the service with HTTP POST method.,

public static void main(String[] args) throws Exception {
 String longUrl = "";

 System.out.println("Long url: " + longUrl+"\n");

 System.out.println("Bitly short url: "+shorten("Bitly", BITLY_KEY, longUrl)+"\n");
 System.out.println("Google short url: " + shorten("Google", GOOGLE_KEY, longUrl));

public static String shorten(String authority, String key, String longUrl) throws Exception {
 if ("Bitly".equals(authority)) {
  String endpoint = ""
   + "/v3/shorten?access_token=" + key
   + "&longUrl=" + longUrl;

  longUrl = longUrl.replace(":", "%3A");
  longUrl = longUrl.replace("/", "%2F");
  longUrl = longUrl.replace("&", "%26");
  longUrl = longUrl.replace(" ", "%20"); url = new; request = ( url.openConnection();

  request.connect(); reader = new

  String serviceResponse = "";
  String line;

  while ((line = reader.readLine()) != null) {
   serviceResponse += line;

  System.out.println("Service response: "+serviceResponse);

  return extractUrl(serviceResponse, "url");
 else if ("Google".equals(authority)) {
  String endpoint = ""
   + "urlshortener/v1/url?fields=id%2ClongUrl&key=" + key;

  String data = "{\"longUrl\": \"" + longUrl + "\"}"; url = new; connection = ( url.openConnection();
  connection.setRequestProperty("Content-Type", "application/json"); output = new;
  output.flush(); response = new

  String serviceResponse = "";
  String line;
  while ((line = response.readLine()) != null) {
   serviceResponse += line;

  System.out.println("Service response: "+serviceResponse);

  return extractUrl(serviceResponse, "id");


 return null;

public static String extractUrl(String serviceResponse, String keyword){
 serviceResponse = serviceResponse.replace(" ", "");
 keyword = "\""+keyword+"\":\"";
 int keywordLength = keyword.length();

 for (int i = 0; i < serviceResponse.length(); i++) {
  if (i < serviceResponse.length() - keywordLength && keyword.equals(serviceResponse.substring(i, i + keywordLength))) {
   for (int j = i + keywordLength; j < serviceResponse.length(); j++) {
    if ("\"".equals(serviceResponse.substring(j, j + 1))) {
     return serviceResponse.substring(i + keywordLength, j);

return null;

So, the following results are produced when the code run. Now, we can involve shortening url service in our business flows. Fortunately, short urls produced by Bitly and Google are non-temporary. Moreover, we would not consider hash collision. In other words, we can produce unique and everlasting urls.

URL Shortening via Bitly and Google

The both Bitly and Google services provide management console to monitor link shortening trafics, quota and click statistics.

Bitly Bitlinks Management Console
Management Console of Google API for URL Shortener

Finally, Bitly limits to shorten 10k links per month for free accounts wheresas providing larger limits for premium accounts. On the other hand, Google limits to shorten 1M link per days. However, Google restricts to shorten 100 links per 100 seconds. That prevents to shorten bulk links. In contrast, Bitly does not restrict traffic in that way.

As mentioned before, Bitly is dedicated firm for url shortening. That’s why, terminating its services would not be expected. However, there are services shut down by Google before. So, I prefer to consume Bitly’s url shortening service for personal use.

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s