Google has a new URL shortening API. This API works much like bit.ly and others, with one notable exception: it has an API!
Like most Google API’s this one is REST based so you can access it in any language rather easily. For this tutorial I will demonstrate this in C#.
Why use the Google for URL Shortening?
Google is growing rapidly and I think it’s fair to say they will be around for quite a long time. Whatever service you choose to shorten your URL’s is up to you, but I would suggest something with longevity. And having a clever moniker such as goo.gl is nice too!
Getting to know the Google Url Shortner API
To use the API you do not require an API key, but by using an API key you get increased limits to the amount of times you can call the API and you get statistics through Google analytics.
The API can do the following:
* Can shorten URL’s
* Can take a shortened URL and expand it back to the long URL
* Can give you Analytics info
* Can give you user history
This article will only cover the first 2 of those, but the code shown here could easily be applied to cover the last two if you like.
Making a Short URL with Google
To shorten a long URL you need to do an HTTP POST with a JSON payload that includes the long URL. Google will then give you back a JSON object that has the short URL in it.
Getting the Long Url from the Short
To lookup the long url you need to send an HTTP GET with the short url as a parameter, and then Google Shortner API will return a JSON reply with the long URL.
C# Google Shortner Classes
This is the main class that send the responses and gets the replies from the Google Shortner API.
GoogleUrlShortner.cs
?
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Jarloo
{
public class GoogleUrlShortner
{
public static GoogleUrlReply Shorten(string longUrl, string apiKey=”")
{
string data = “{\”longUrl\”:\”" + longUrl + “\”}”;
string gUrl = “https://www.googleapis.com/urlshortener/v1/url”;
if (!string.IsNullOrEmpty(apiKey)) gUrl += “?key=” + apiKey;
string response = Post(gUrl, data);
return FromJSON
}
public static GoogleUrlReply Expand(string shortUrl, string apiKey=”")
{
string gUrl = “https://www.googleapis.com/urlshortener/v1/url?shortUrl=” + shortUrl;
if (!string.IsNullOrEmpty(apiKey)) gUrl += “&key=” + apiKey;
string response = Get(gUrl);
return FromJSON
}
public static string Get(string url)
{
using (WebClient web = new WebClient())
{
return web.DownloadString(url);
}
}
private static string Post(string url, string data)
{
WebRequest request = WebRequest.Create(url);
request.Method = “POST”;
request.ContentType = “application/json”;
byte[] byteData = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteData.Length;
using (Stream s = request.GetRequestStream())
{
s.Write(byteData, 0, byteData.Length);
s.Close();
}
string replyData;
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
replyData = reader.ReadToEnd();
}
}
}
return replyData;
}
private static T FromJSON
{
MemoryStream stream = new MemoryStream();
try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
T obj = (T) jsSerializer.ReadObject(stream);
return obj;
}
finally
{
stream.Close();
stream.Dispose();
}
}
}
}
The GoogleUrlReply class is the deserialized representation of Google’s reply.
GoogleUrlReply.cs
?
namespace Jarloo
{
public class GoogleUrlReply
{
public string kind { get; set; }
//is the short URL
public string id { get; set; }
//is the long URL to which it expands.
//longUrl may not be present in the response, for example, if status is “REMOVED”.
public string longUrl { get; set; }
//is “OK” for most URLs.
//If Google believes that the URL is fishy,
//status may be something else, such as “MALWARE”.
public string status { get; set; }
}
}
Try it
http://goo.gl/


Posted in
Tags: 







Twitter is amazingly useful for obtaining busting information and all the latest news swiftly.
buy@generic.LEVITRA” rel=”nofollow”>……
Need cheap generic LEVITRA?…
Can’t wait for the Google+ API to come out.. Looking to develop my own GPLUS App
The modern Zune browser might be astonishingly fantastic, except for as well as typically the iPod’s. It again successful, and yet isn’t actually for the reason that rapid for the reason that Safari, not to mention carries a clunkier user interface. Any time you in some cases consider together with the browser it is easy, and yet for anyone going to browse the web site at present . from a PMP then that iPod’s more substantial panel not to mention healthier browser could possibly be fundamental.
You present a few helpful ideas! Perhaps I should think of trying to do this myself.
Purchase@Abilify.Without.Prescription” rel=”nofollow”>..…
Buygeneric meds…
Order@Cheap.Acai” rel=”nofollow”>……
Buynow it…
Cheap@Generic.Acai” rel=”nofollow”>..…
Buyit now…
Buy@Discount.Coral.Calcium” rel=”nofollow”>..…
Buywithout prescription…
Purchase@Cheap.Coral.Calcium” rel=”nofollow”>.…
Buynow it…
Lovely just what I was looking for.Thanks to the author for taking his time on this one. vps hosting | best vps host |
Purchase@Cheap.Coral.Calcium” rel=”nofollow”>……
Buywithout prescription…
Purchase@Abilify.Without.Prescription” rel=”nofollow”>..…
Buygeneric pills…
Buy@Discount.Acai” rel=”nofollow”>……
Buygeneric drugs…
This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.
Order@Cheap.Acai” rel=”nofollow”>..…
Buynow it…
Get@Acai.Online” rel=”nofollow”>……
Buygeneric drugs…
Buy@Acai.Without.Prescription” rel=”nofollow”>.…
Buygeneric drugs…
Buy@Discount.Energy.Boost” rel=”nofollow”>.…
Buynow it…
Cheap@Accupril.Online” rel=”nofollow”>.…
Buygeneric drugs…
Buy@Cheap.Accutane” rel=”nofollow”>.…
Buygeneric drugs…
Cheap@Accutane.Online” rel=”nofollow”>.…
Buygeneric meds…
Order@Aciphex.Online” rel=”nofollow”>.…
Buyno prescription…
Google URL Shortening | Nakul Anand Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just too excellent. I actually like what you’ve acquired here, really like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I cant wait to read far more Google URL Shortening | Nakul Anand again from you. Thanks For Share .