Shorten your URLs using goo.gl

Name: *
My email: *
Recipient email: *
Message: *
Fields marked as bold are compulsory.
You haven't filled in compulsory values. The email is not correct

Shortened URLs have slowly turned into one of the latest trends. Even though it's been about 15 years they've been helping people having difficulty with limited character messaging applications and SMS, social networks sharing such limitations have caused some major increment in their use. We are going to see what shortened URLs are, and how we can create our own, using goo.gl API.
 

What is a shortened URL?

 
A shortened URL is a URL that redirects to the same address as the one requested. For example you can use goo.gl (Google's URL shortener service) to create a shortened URL by visiting https://goo.gl/ and placing the URL you want to shorten. If you place http://dotnethints.com/  you get http://goo.gl/5nIcLC.
 
Now, if you hit that new address on your browser you will head to that URL shortener's company site. The original URL will be found and you will be automatically redirected to your destination. Keep in mind that shortened URLs are supposed to last forever.
 
So, we may use shortened URLs when we want to minimize the number of characters contained within our URL. Shortened URLs can also be used if we want to hide the address our original URL is pointing to from the person to click it. This is the reason many spammers choose to use URL shortening.
 
There are many URL shortening services out there, including bitly and tiny URL. goo.gl, the service supported by Google, is the one we will be using in this article.
 

Using goo.gl

goo.gl
 
As mentioned earlier, to use goo.gl a single person can use the service in the https://goo.gl/ page.
 
A developer can also make use of the goo.gl API to create a JSON request containing the original URL and get a JSON response containing the shortened URL.
 
To use the goo.gl API a developer needs to have an API key. You can get a key in this page 
 
Now that we have our key, we need to create our request to https://www.googleapis.com/urlshortener/v1/url?key=my_API_key and deal with the results so we get the shortened URL.
 
To accomplish this we can use the following method:
 
 
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=my_API_key");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
 
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
   string json = "{\"longUrl\":\"" + url + "\"}";
   streamWriter.Write(json);
}
 
We create a POST request containing a JSON object with a longUrl element. That is the original URL we want to shorten.
 
Then we will get a JSON response containing a serialized object (in the form of the UrlShortener class we create) containing three elements: kind, id and longUrl.
 
kind  is the kind of response we get eg urlshortener#url
longURL is the original URL we want shorten
id is the shortened URL.
 
    private string GetShortenedURL(string url)
    {
        var shortenedURL = url;
 
        //Create the request
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=my_API_key");
       httpWebRequest.ContentType = "application/json";
       httpWebRequest.Method = "POST";
 
      //Add longURL
      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
      {
         string json = "{\"longUrl\":\"" + url + "\"}";
         streamWriter.Write(json);
      }
 
 
 
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            //Deserialize response
            var responseText = streamReader.ReadToEnd();
            var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            jsonSerializer.MaxJsonLength = 2147483647;
            UrlShortener shortener = jsonSerializer.Deserialize<UrlShortener>(responseText);
 
            shortenedURL = shortener.id;
        }
 
        return shortenedURL;
    }
 
    private class UrlShortener
        {
            public string kind;
            public string id;
            public string longUrl;
        }
 
 
For example if I used the code above to ask for the http://dotnethints.com/ shorten URL, I would get the following JSON
{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/RWZBj0",
 "longUrl": "http://dotnethints.com/"
}
 
Since a serialized JSON is no more than a string we can also get its id by using string methods, but I personally find this way to be a better one.
 

Summary

Shortened URLs are URLs that can be used in place of the original longer ones to minimize their length. There are many services we can use to create a shortened URL, goo.gl being one of them. We can either visit goo.gl's home page to create shortened URLs on the fly or use the goo.gl API to create JSON requests and get responses containing the info we want.
 
 
 

Back to BlogPreviousNext

Comments


  • 23-07-2016, 20:45 PM
    kbadas
    Posts: 6
    Yes, this is right. Similar to the example I have given, using the http://goo.gl/5nIcLC shortened URL, will automatically lead you to the original http://dotnethints.com page.
  • 23-07-2016, 16:21 PM
    krishnan
    Posts: 1
    goo.gl convert long url to short url but it will visit the original log url when convert original long url to short url. ?

Leave a comment
Name: