This is a mirror of official site: http://jasper-net.blogspot.com/

Google Shortener API For .NET Written In C#

| Sunday, January 16, 2011
Google Shortener is a service provided by Google to create short URLs. Google Shortener has been available for some time now but only recently Google released a public API to access this service programmatically. Today I decided to try it out and wrote some C# code to work with the API. I will describe what I did in this post.

A bit about the API

Like other URL shorteners, Google API also takes in a long URL and gives you back a short one. When you click on the short URL, you end up at the same location on web as you did with your original long URL.
The API itself is implemented as a RESTful service and is very simple. You can read the overview and reference for API here.

.NET Wrapper

I wanted to use the API in my .NET application so I created a wrapper. I have made the source code available at code.google.com. Feel free to download and play with it.
There are three methods in Google Shortener API:

url.insert – Shrinks a Url.
url.get – Returns the long Url for a valid short url.
url.list – Returns a list of Urls shortened by an authenticated user. Analytics data is also returned by this method.

If you want analytics for your Url then you must invoke the methods by passing in an API key. You can get your API key from Google API Console.
Enough talk. Let me show you the code. The main class in the wrapper is called Shortener which contains two methods: GetShortUrl() and GetLongUrl(). An API key can be passed in to the constructor of Shortener and the API key will be used for all subsequent requests for that instance.

public Reply GetShortUrl(string longUrl)
{
 string data = "{\"longUrl\":\"" + longUrl + "\"}";
 string postUrl = googleShortenerUrl;
 string response = HttpHelper.HttpPOST(postUrl, data);
 return DeserializeJSON(response);
}

public Reply GetLongUrl(string shortUrl)
{

Read more: Therefore Systems

Posted via email from .NET Info

0 comments: