Shortening Your Long URLs with the TinyURL API and .NET


The world is becoming lazier. It gets easier to do certain tasks, and certain previously tedious tasks now get done for us. Technology is great! With new technology comes new solutions, as in the case of today's topic. Yes, TinyURL has been around a very long time, but you may not have heard about it before, or, you may have been wondering how to shorten your long URLs quickly as TinyURL does. Today, I will show you. It is quite easy and not a lot of work, so let's get started, shall we?


Create a new C# or Visual Basic.NET Windows Forms Application and design your form to resemble Figure 1.

Figure 1: Design

Add the ShrinkURL method.


   private string ShrinkURL(string strURL)

      string URL;
      URL = "http://tinyurl.com/api-create.php?url=" +

      System.Net.HttpWebRequest objWebRequest;
      System.Net.HttpWebResponse objWebResponse;

      System.IO.StreamReader srReader;

      string strHTML;

      objWebRequest = (System.Net.HttpWebRequest)System.Net
      objWebRequest.Method = "GET";

      objWebResponse = (System.Net.HttpWebResponse)objWebRequest
      srReader = new System.IO.StreamReader(objWebResponse

      strHTML = srReader.ReadToEnd();


      return (strHTML);



   Private Function ShrinkURL(ByVal strURL As String) As String

      Dim URL As String
      URL = "http://tinyurl.com/api-create.php?url=" + _

      Dim objWebRequest As Net.HttpWebRequest
      Dim objWebResponse As Net.HttpWebResponse

      Dim srReader As IO.StreamReader

      Dim strHTML As String

      objWebRequest = CType(Net.WebRequest.Create(URL), _
      objWebRequest.Method = "GET"

      objWebResponse = CType(objWebRequest.GetResponse(), _
      srReader = New IO.StreamReader(objWebResponse _

      strHTML = srReader.ReadToEnd


      Return (strHTML)

   End Function

The ShrinkURL function generates a shortened URL with the help of the 'api-create' method in its URL. You supply the long URL that was entered in one of the Textboxes; then, you need to create WebRequest objects to obtain the returned shortened URL and a StreamReader object to interpret the URL and return a properly formed string to be returned to the calling method or procedure.

Add the code for the Process and Copy buttons.


   private void btnProcess_Click(object sender, EventArgs e)

      txtOutput.Text = ShrinkURL(txtURL.Text);
      txtURL.Text = "";


   private void btnCopy_Click(object sender, EventArgs e)




   Private Sub btnProcess_Click(sender As Object, e As EventArgs) _
         Handles btnProcess.Click

      txtOutput.Text = ShrinkURL(txtURL.Text)
      txtURL.Text = ""

   End Sub

   Private Sub btnCopy_Click(sender As Object, e As EventArgs) _
         Handles btnCopy.Click


   End Sub

The Process button waits a second, and then adds the shrunken URL to the Output Textbox. The Copy button simply copies the URL to the Clipboard.

Figure 2 shows a long URL that was entered. Figure 3 shows the shortened URL.

Long URL
Figure 2: Long URL

Short URL
Figure 3: Short URL


Quick and dirty. I just want to thank everyone for reading my articles. Some articles are quite long, some are short, but I do hope you benefit from them. My aim with these articles is to help you learn funky tricks, or interesting things, or simply learn something new. I know what it is like to struggle and not know what to do and where to go and what to look for. CodeGuru has helped so much! I just hope my ideas and my experience help you as well.

This article was originally published on October 18th, 2018

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date