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.
Sometimes I get error: 403 – RATE_LIMIT_EXCEEDED. And ApiKey is deprecated.
Good, great post.
any good patterns and practices about it? any Nuget ? thx
Thanks CooLMinE. I’ll check it :)
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?
If something goes wrong on bitly’s side (including authentication) you will get all the information from the status code and status text.
For additional information on what kind of status codes you can expect, have a look at http://dev.bitly.com/formats.html
Very nice article, exactly what I wanted to find.
very good guide , this really help. thank you
This site was… how do I say it? Relevant!! Finally I have found something which helped me. Thanks a lot!
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.
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.
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!