•   Posted in: 
  • C#

imageWhile building an e-commerce web application, I realized that every e-commerce site needs social media button on the product detail page so that visitors can share what they have found. Of course you can let them share an URL like:


But since the twitter limit of 140 chars and the URL having 57, you can only share a message of 82 chars (with space) which is kind of short.

ps. when URL rewriting, consider using a dash instead of plus or underscore character

So in order to have good URL’s for social sharing, you should shorten your URL’s

I have made a test with Asp.Net C# comparing the famous bit.ly and goo.gl (which officially only works from the Google toolbar) [more]


For Bit.ly, I used this DLL from this website: http://sites.google.com/site/bitlyapp/

After I included it in my project, I made a helper method:

internal static string GetBitLy(string urlToShorten)
    string urlShort = "";
    if (urlToShorten.Length > 0)
            urlShort = Bitly.API.Bit(Util.GetConfig("BitlyUser"), 
        catch (Exception ex)
            // magic errorlogging goes here
    if (urlShort.Length > 0) // this check is if your bit.ly rate exceeded
        return urlShort;
        return urlToShorten;

My Util.GetConfig does:


Only then with Caching and error handling and it saves me typing a few characters.


I have found some code in a comment of a blog. The comment pointed to this URL. I have uploaded my class here GoogleUrlShortner.cs (4.17 kb) as mirror.

The test

Here is my code for the test:

public string GetShortUrl()
    string shortUrl = "http://www.mydomain.com/cars/audi/Audi-R8-V12-TDI-Le-Mans";
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    shortUrl = Util.GetBitLy(shortUrl);
    System.Diagnostics.Debug.WriteLine("bit.ly: " + sw.ElapsedTicks + " and url " + shortUrl);
    shortUrl = shop.BLL.Utility.GoogleUrlShortner.Shorten(shortUrl);
    System.Diagnostics.Debug.WriteLine("goo.gl: " + sw.ElapsedTicks + " and url " + shortUrl);
    shortUrl = Server.UrlEncode(shortUrl);
    return shortUrl;

As you've noticed, I used the Stopwatch class which is great for measuring performance of your code.

The result

Here is the output.

bit.ly: 1988350 and url http://bit.ly/cLkqPZ
goo.gl: 1062865 and url http://goo.gl/DrFx

Second run:

bit.ly: 1125702 and url http://bit.ly/cLkqPZ
goo.gl: 674658 and url http://goo.gl/DrFx


Bit.ly has a great benefit that you can track the clicks of a shortened URL. but when using URL shortening on a product detail page, it’s useless when you have about 3000 product detail pages.

So I’d chose to go with Goo.gl since it has a faster response time and less characters in the shortened URL’s

I got a 403 sometimes...
The remote server returned an error: (403) Forbidden.
anyone an idea?

kick it on DotNetKicks.com Shout it

Update 11-jan-2011: Google URL Shortner gets API!

Pin on pinterest Plus on Googleplus Post on LinkedIn


Comment by TJ Wrenn

Thanks for the code. The format of the JSON response from goo.gl changed slightly. I revised the regex to account for the change.

return Regex.Match(json, @"{""short_url"":""([^""]*)""[,}]").Groups[1].Value;

TJ Wrenn
Comment by capsoft

Thanks TJ Wrenn for your update!

Comment by Shorten Hyperlinks

Very useful code to share. I use http://www.Gog.li shorten hyperlinks service which provides real time link tracking service totally free.

Shorten Hyperlinks
Comment by Yousef

I used your code for goo.gl, but I got this exception when calling GetResponse.

The remote server returned an error: (401) Unauthorized.

Kinldy advice.