How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Shortening a URL using bitly’s API in C#

Using the snippet below you can convert links in your application from this

to this

This is especially important in cases where you have a fairly long URL which you want to post somewhere and you either don’t want to use such a long URL, or simply you are limited by characters. An example of that situation would be a URL such as

which can be shortened down to

The first thing you are going to need it an account from bitly.com. After you have created an account, log in and navigate to https://bitly.com/a/your_api_key. The page will contain your username and your API key which are both needed.

Here is the snippet which you will need to modify to use your own username and API key

Keep in mind since bitly’s API supports both POST and GET methods this could be rewritten to something shorter using the WebClient class. As an example:

The reason why I prefer the first approach is because it gives your more control over to what is happening, which makes its easier to add various features later on down the line if you want something like a progress bar that shows how long it will take for the task to complete and so on.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

12 Responses to Shortening a URL using bitly’s API in C#

  1. kiquenet says:

    Sometimes I get error: 403 – RATE_LIMIT_EXCEEDED. And ApiKey is deprecated.

  2. Deângelo says:

    Good, great post.

  3. pregunton says:

    any good patterns and practices about it? any Nuget ? thx

  4. Perunchithiran says:

    Thanks CooLMinE. I’ll check it :)

  5. Perunchithiran says:

    Thanks it really works. This is what I was looking for. If at all any exception comes how to handle it. Is there any exception class that is provided by bitly API?

  6. Cora H. says:

    Very nice article, exactly what I wanted to find.

  7. Steven Tong says:

    very good guide , this really help. thank you

  8. Grec says:

    This site was… how do I say it? Relevant!! Finally I have found something which helped me. Thanks a lot!

  9. tom tolog says:

    Hello there, You have done an excellent job. I will certainly digg it and personally suggest to my friends. I am sure they will be benefited from this site.

  10. tonyahuntington says:

    Useful info. Lucky me I found your website by chance, and I am stunned why this coincidence did not took place earlier! I bookmarked it.

  11. Arty says:

    Very great post. I simply stumbled upon your weblog and wished to say that I’ve truly enjoyed surfing around your weblog posts. After all I will be subscribing in your feed and I am hoping you write again soon!

Leave a Reply

Your email address will not be published. Required fields are marked *