Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Internet & Web » Consuming URL Shortening Services - Cligs

Consuming URL Shortening Services - Cligs

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.

Author Rank :
Page Views : 2264
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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



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

Enough talking, let's begin the discussion.

In December 2009, Cligs 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 need 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 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 you.) The last step generates an API key that's no 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 choose above each of the URLs.


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#

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
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#

HTML clipboarstring 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
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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mohammad Elsheimy
Independent software developer, trainer, and technical writer from Egypt. Student in Holy Quranic Sciences college, Azhar University.

Blogs

Follow me @elsheimy on Twitter, and don't forget to say hello in our page in Facebook.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
cool! by Mike On September 2, 2010
Thanks Mohammad, that's a thorough explanation of how to use CLIGS in .NET.
Reply | Email | Modify 
Re: cool! by Mohammad On September 2, 2010
You're welcome! :)
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.