Shorten your URLs using

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 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 (Google's URL shortener service) to create a shortened URL by visiting and placing the URL you want to shorten. If you place  you get
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., the service supported by Google, is the one we will be using in this article.

As mentioned earlier, to use a single person can use the service in the page.
A developer can also make use of the API to create a JSON request containing the original URL and get a JSON response containing the shortened URL.
To use the 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 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("");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
   string json = "{\"longUrl\":\"" + url + "\"}";
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("");
       httpWebRequest.ContentType = "application/json";
       httpWebRequest.Method = "POST";
      //Add longURL
      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
         string json = "{\"longUrl\":\"" + url + "\"}";
        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 =;
        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 shorten URL, I would get the following JSON
 "kind": "urlshortener#url",
 "id": "",
 "longUrl": ""
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.


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, being one of them. We can either visit's home page to create shortened URLs on the fly or use the API to create JSON requests and get responses containing the info we want.

Back to BlogPreviousNext


  • 23-07-2016, 20:45 PM
    Posts: 6
    Yes, this is right. Similar to the example I have given, using the shortened URL, will automatically lead you to the original page.
  • 23-07-2016, 16:21 PM
    Posts: 1 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