Click here to Skip to main content
15,867,986 members
Articles / Programming Languages / Visual Basic

Consuming URL Shortening Services – is.gd

Rate me:
Please Sign up or sign in to vote.
4.38/5 (7 votes)
21 Sep 2010CC (Attr 3U)2 min read 25K   13   3
Another article of our series that talks about accessing URL shortening services programmatically. This article is talking about is.gd shortening service, how you can use it, and how to access it via your C#/VB.NET application.
هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.
Read more about URL Shortening Services here.

Source- Elsheimy.Samples.ShortenSvcs.zip

Contents

Contents of this article:
  • Contents
  • Overview
  • Introduction
  • API
  • What's next

Overview

Another article of our series that talks about accessing URL shortening services programmatically.

This article is talking about is.gd shortening service, how you can use it, and how to access it via your C#/VB.NET application.

Introduction

is.gd is one of the most popular shortening services ever in the web because of its simple interface and its easy-to-use API.

When you visit service website, http://is.gd, you can see that nothing easier from is.gd, just push your long URL into the text box and click the shortening button.

API

is.gd provides you a very simple easy-to-use API. This API contains only one function that's used for shortening URLs. Another good thing is that this function doesn't require any kind of authentication for users. Therefore, you need just to spam it with your long URL (as you did with the website.)

This glorious function is called http://is.gd/api.php, it accepts only a single argument, longurl, which can be set to the long URL you need to shorten. When you call the function, it simply returns the shortened URL as plain text (no more overhead.)

Now, let's try this function. We'll try to shorten the URL http://JustLikeAMagic.com with our function. First, connect the arguments, http://is.gd/api.php?longurl=http://JustLikeAMagic.com. Now copy this address and paste it into your favorite browser. If everything was OK, you should see the short URL after clicking €˜Go' in the browser toolbar.

Now, let's do it in C# and VB.NET. Check the following function that tries to shorten long URLs via the id.gd API:

C#
// C#

string Shorten(string url)
{
    url = Uri.EscapeUriString(url);
    string reqUri = String.Format(@"http://is.gd/api.php?longurl={0}", url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 5000;


    try
    {
        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
VB.NET
' VB.NET

Function Shorten(ByVal url As String) As String

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = _
        String.Format("http://is.gd/api.php?longurl={0}", url)
    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()
        reader.Dispose()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try

End Function

Notice that we have used the function System.Net.Uri.EscapeUriString() to eliminate unacceptable characters from the URL by encoding them.

Notice too that we have included our code in a Try-Catch block so we can catch exceptions before they blow up our application.

What's next

Consider reading other articles in this series here.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
GeneralMy vote of 4 Pin
stikves19-Sep-10 13:20
stikves19-Sep-10 13:20 
GeneralRe: My vote of 4 Pin
Mohammad Elsheimy20-Sep-10 1:15
Mohammad Elsheimy20-Sep-10 1:15 
GeneralMy vote of 4 Pin
stikves19-Sep-10 13:19
stikves19-Sep-10 13:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.