Click here to Skip to main content
15,883,889 members
Articles / Programming Languages / Visual Basic

Consuming URL Shortening Services – Cligs

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
21 Sep 2010CC (Attr 3U)4 min read 26.2K   4   1
This is another article that talks about URL shortening services. Today we are going to talk about Cligs, one of the popular shortening services on the web.
هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.
Read more about URL shortening services here.

Contents

  • Overview
  • Introduction
  • Description
  • API
    • Shortening URLs
    • Expanding URLs
  • Where to go next
  • History

Overview

This is another article that talks about URL shortening services. Today, we are going to talk about Cligs, one of the popular shortening services on the web.

Be prepared!

Introduction

Image 1

Today we are talking about another popular shortening service; it’s Cligs, one of the most popular shortening services that provides lots of premium features for FREE.

Enough talking, let’s begin the discussion.

In December 2009, Cligs was acquired by Mister Wong (a very nice bookmarking service).

Description

How Cligs can help you? Cligs gives you plenty of features, including the following:

  • Shortening URLs (registered and non-registered users): You get a short URL that’s no more than 20 characters (Tweetburner is 22 and is.gd is only 18) including the domain http://cli.gs.
  • URL Management (registered users only): It allows you to manage your short URLs, to edit them, and to remove them if you like.
  • Real-time Analytics (registered users only): How many clicked your link, and when.
  • URL Previewing (registered and non-registered users): Preview the URL before opening it. Protects you from spam and unwanted sites.

API

Cligs provides you a very nice API with many advantages. The first advantage that we want to talk about is its simplicity. The API is very simple; it has just two functions, one for shortening URLs, and the other for expanding short URLs (to expand a URL means to get the long URL from the short one.)

Another advantage of this API is that it allows you to shorten the URLs whether you are a registered user or not. Of course, a registered user needs to get an API key in order to link the API calls to his accounts so he can manage the links generated by the API and to watch the analytics.

Shortening URLs

The first function is used for shortening URLs and it’s called, create. This function has the following address:

http://cli.gs/api/v1/cligs/create?url={1}&title={2}&key={3}&appid={4}

The API is still in version 1, that’s why you see ‘v1’ in the address. This function takes four parameters, only the first one is required, other parameters are used for authentication:

  1. url: Required. The URL to be shortened.
  2. title: Optional. For authenticated calls only. The name that would be displayed on the short URL in your control panel (used for managing your URLs.)
  3. key: Optional. If you are a registered user and you want to link the API calls to your account, you’ll need to enter your API key here.
  4. appid: Optional. If you have used an API key, then you have to provide your application name that was used to generate this API call (help users know the source of the link.)

So how can you use this function? If this is an anonymous call (i.e., no authentication details provided), just call the function providing the long URL in its single required argument.

If you need to link this call to a specific account, then you’ll need an API key, which the user can get by signing in to his Cligs account, choosing ‘My API Keys’, then clicking ‘Create New API Key’ (if he doesn’t have one). The last step generates an API key that’s not exactly 32 characters (see figure 1.)

Figure 1 - Creating API Keys, Cligs

After you get the API key, you can push it to the function along with your application name in the appid argument.

What about the title argument? For registered users, they can access their clig list (see figure 2) and see the URLs they shortened and the titles they chose above each of the URLs.

Image 3

Now, let’s code! The following function makes use of the Cligs API to shorten URLs. It accepts three arguments, the long URL, the API key, and the application name. If the API key is null (Nothing in VB.NET,) the call is made anonymously, otherwise, the API key and the application name are used.

C#
// C#

string Shorten(string url, string key, string app)
{
    url = Uri.EscapeUriString(url);
    string reqUri =
        String.Format(@"http://cli.gs/api/v1/cligs/create?url={0}", url);
    if (key != null)
        reqUri += "&key=" + key + "&appid=" + app;

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 5000;

    try
    {
        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
VB.NET
' VB.NET

Function Shorten(ByVal url As String, _
                 ByVal key As String, ByVal app As String) As String

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = _
        String.Format("http://cli.gs/api/v1/cligs/create?url={0}", url)
    If key Is Nothing Then
        reqUri &= "&key=" & key & "&appid=" & app
    End If

    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()
        reader.Close()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try

End Function

Expanding URLs

The other function we have today is the expand function that’s used to get the long URL from the short one (e.g. to expand the short URL http://cli.gs/p1hUnW to be http://JustLikeAMagic.com.) This function is very simple and it has the following address:

http://cli.gs/api/v1/cligs/expand?clig={1}

This function accepts only a single argument, that’s the clig (short URL) to be expanded. The clig can be specified using one of three ways:

  • The clig ID, e.g. p1hUnW
  • The raw URL, e.g. http://cli.gs/p1hUnW
  • The encoded URL, e.g. http%3A%2F%2Fcli.gs%2Fp1hUnW
You can read more about URL encoding here.

Now it’s the time for code! The following function takes a clig and returns its original URL:

C#
// C#

string Expand(string url)
{
    url = Uri.EscapeUriString(url);
    string reqUri = String.Format(@"http://cli.gs/api/v1/cligs/expand?clig={0}", url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 5000;

    try
    {
        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
VB.NET
' VB.NET

Function Expand(ByVal url As String) As String

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = _
        String.Format("http://cli.gs/api/v1/cligs/expand?clig={0}", url)

    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()
        reader.Close()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try

End Function

Where To Go Next

Some other articles about URL shortening services are available here.

History

  • 21st September, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA22-Mar-24 23:51
professionalȘtefan-Mihai MOGA22-Mar-24 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.