Click here to Skip to main content
Click here to Skip to main content

Consuming URL Shortening Services - Introduction

, 21 Sep 2010
Rate this:
Please Sign up or sign in to vote.
This is the first article of our series that talks about accessing URL shortening services programmatically. Here we introduce new concepts like the REST API. We also have a brief discussion of URL shortening services APIs and how you can access them. In addition, we are going to talk about .NET sup

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا. 

Read more about URL shortening services here.

Source- Elsheimy.Samples.ShortenSvcs.zip

Contents

Contents of this article:
  • Contents
  • Overview
  • Introduction
  • Accessing the API
  • Authentication
  • .NET Support
  • Encoding
  • Sample
  • Where to Go Next
  • Poll: What are your favorite URL shortening services?

Overview

This is the first article of our series that talks about accessing URL shortening services programmatically.

Here we introduce new concepts like the REST API. We also have a brief discussion of URL shortening services APIs and how you can access them.

In addition, we are going to talk about .NET support for the REST API and tools and techniques available that would help us during our journey through the API.

A working example built using C# and WinForms is available at the end of this article.

This article is the base for all other articles. Articles other than this discuss specific services and their APIs. We will make use of code and techniques discussed here throughout the rest of articles.

Let's go!

Introduction

URL shortening services are very popular these days since web addresses tend to be very long and very difficult to be exchanged via email or other micro-blogging services like Twitter.

Today, there're tenths of URL shortening services spread out all over the web. Most of them are listed in the following articles:

In this series, we are going to talk about how to consume URL shortening services in your .NET application. In other words, we will discuss how to program URL shortening services; we mean how to access them programmatically. In addition, we will have a brief discussion of each service and its unique features.

How we will go through this discussion? This is going to be a series of articles published regularly. Each article discusses a given shortening service from the following list (updated regularly, expect new services to be added):

The preceding list may not be comprehensive, many other popular services exist. However, not all shortening services have APIs! The preceding list contains the shortening services we know that allow developers to consume their functionalities via an exposed API.

Before we start with a specific service, let's have a brief discussion of features of the API and how we can access them.

Accessing the API

Most APIs in the web are just REST (Representational State Transfer) web services. A REST web service is simply a collection of functions (HTTP endpoints) that can be used to retrieve data in a variety of formats (optional.)

Given an endpoint (function) like http://ab.c/api/shrink, we could supply the required input arguments as query strings in the URL. For example, we could shorten the URL www.google.com using a call to the HTTP endpoint http://ab.c/api/shrink?url=www.google.com supplied with the required information. It is worth mentioning that every service has its own API functions and arguments. Although they all do the same thing (shortening the URL,) they differ in function and argument names.

When you call a web function, you just end up with the results in the format used in that function (XML, Atom, JSON, etc.) The function could also return plain text! It's all about the function documentation that you should check carefully before calling the function. In addition, the returned value from a function may also contain error details if the function didn't success.

Keep in mind that API calls are limited and you can't just leave the shortening code in an end-less loop! Other limitations of specific APIs should be discussed later.

Authentication

Most URL shortening services allow users to consume the service without being registered, some of allow users to register, and others not. Many other services require users to be registered in order to use the service.

Likewise, service APIs may or may not require user authentication. Some services give the user an API key that can be used in the authentication process. Other services require user to enter his login details in the API calls. Most use the API key approach.

.NET Support

Does .NET support the REST API? Sure! As long as REST web services are just collections of HTTP endpoints, we do not need to worry about accessing them from .NET since that the BCL offers us a bunch of classes (available in System.dll) that can help with HTTP requests and responses.

In addition, we will rely heavily on classes of System.Xml.dll to handle the data returned from functions (we will make use of functions that support XML.)

If we could write something in C# that calls our fake web function http://ab.c/api/shrink, we could end up with some code like this:

// C#

public void string Shorten(string url)
{
    string reqUri = @"http://ab.c/api/shrink?url=" + url;

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

    XmlDocument doc = new XmlDocument();
    doc.Load(req.GetResponse().GetResponseStream());

    return HandleXml(doc);
}
' VB.NET

Function Shorten(ByVal url As String) As String

    Dim reqUri As String = "http://ab.c/api/shrink?url=" & url

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

    Dim doc As new XmlDocument()
    doc.Load(req.GetResponse().GetResponseStream())

    Return HandleXml(doc)

End Function

Encoding

If the URL to shorten have special characters like '&', '?', '#', or ' ', those characters should be handled first before the URL is sent to the shortening function.

This special handling for URLs is usually called Percent-encoding or URL Encoding. This encoding replaces unsafe characters with their hexadecimal values preceded by percentage ('%') signs.

There are many unsafe characters that should be percent-encoded including '$', '+', '&', ':', and '='. A nice discussion of URL Encoding can be found in the article URL Encoding by Brian Wilson.

Why unsafe characters are problematic? Consider the following example: we need to shorten the URL http://justlikeamagic.com/?s=twitter with our fake shortening service http://ab.c, we end up with a call to the function using the following address http://ab.c/api/shrink?url=http://justlikeamagic.com/?s=twitter. Now we have two '?'s!!!

So how can we encode URLs? Simply use the EscapeUriString() function of System.Net.Uri class it's adequate for us now.

Sample

Download links for the sample application will be available in just a few days, once we publish some other articles. Smile | :)

Where to Go Next

Now start with any shortening service you like (links are update regularly, new services will be available too):

Or else, check our URL Shortening Services tag.

License

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

Share

About the Author

Mohammad Elsheimy
Technical Writer Just Like a Magic
Egypt Egypt
Independent technical writer from Egypt born in 1991.
 
I am a student of The Holy Quranic Sciences Institute. There, we study the Holy Quranic sciences and Islamic legislation.
 
I currently run two sites:
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pinmembervalery possoz15-Jun-11 2:48 
GeneralMy vote of 5 PinmemberKim Togo29-Sep-10 22:22 
GeneralMy vote of 5 Pinmemberkanu@adatapost26-Sep-10 15:25 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 21 Sep 2010
Article Copyright 2010 by Mohammad Elsheimy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid