JP Hellemons Building E-commerce web applications

Shorten your URL with Goo.gl or Bit.ly in Asp.Net C#

1. September 2010 08:04by JP Hellemons 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:

http://www.mydomain.com/cars/audi/Audi-R8-V12-TDI-Le-Mans

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)

Bit.ly


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)
{
try
{
urlShort = Bitly.API.Bit(Util.GetConfig("BitlyUser"), 
Util.GetConfig("BitlyApiKey"), 
urlToShorten, 
"Shorten");
}
catch (Exception ex)
{
// magic errorlogging goes here
}
}
if (urlShort.Length > 0) // this check is if your bit.ly rate exceeded
return urlShort;
else
return urlToShorten;
}

My Util.GetConfig does:

ConfigurationManager.AppSettings.Get(key);

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

Goo.gl


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();
sw.Start();
shortUrl = Util.GetBitLy(shortUrl);
sw.Stop();
System.Diagnostics.Debug.WriteLine("bit.ly: " + sw.ElapsedTicks + " and url " + shortUrl);
sw.Restart();
shortUrl = shop.BLL.Utility.GoogleUrlShortner.Shorten(shortUrl);
sw.Stop();
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

Conclusion


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


Edit:
I got a 403 sometimes...
System.Net.HttpWebRequest.GetResponse()
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!

Share or Bookmark this post…
  • del.icio.us
  • Digg
  • DotNetKicks
  • eKudos
  • E-Mail
  • Facebook
  • Google
  • LinkedIn
  • msdn Social
  • Reddit
  • NuJIJ
  • Slashdot
  • TwitThis
  • StumbleUpon

Comments (4) -

TJ Wrenn 4/20/2011 10:37:25 PM United States #
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;

Reply

capsoft 4/21/2011 10:08:48 AM Netherlands #
capsoft

Thanks TJ Wrenn for your update!

Reply

Shorten Hyperlinks 10/4/2011 7:36:15 AM #
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.

Reply

Yousef 7/5/2012 11:29:42 AM Jordan #
Yousef

Thanks.
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.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading