TinyURL & .NET

Ever want to parse a TinyURL in your .NET application? TinyURL has a very simple and easy to use API. I’ll guide you through the process of returning a TinyURL in VB.NET 2008 and C# .NET 2008

First off you want to create a function that returns a string.
Simple enough, this function ask for the parameter of URL and will return a string. 

Public Function GetTinyUrl(ByVal URL As String) As String
End Function

Next you want to make sure you’re not using the TinyURL service for no reason. The API is designed for links over 30 characters long. So if you have a 20 character URL TinyURL will end up giving you a 20-35 character URL. So let’s check this first. We’re going to use the Try/Catch method incase something might go wrong. If so, we’ll return the users URL back to him.

Try
    If Url.Length <= 30 Then
       Return URL
     End If
Catch
       Return Url
     End Try

Next we are going to check our link to make sure it starts with “http”.

If Not Url.ToLower().StartsWith("http") AndAlso Not Url.ToLower().StartsWith("ftp") Then
  Url = "http://" + Url
End If

Now that we have that done, lets declare some variables. “req” is used to create  our request to use the API, “rsp” is used to get the response from our request. Simple enough, right? “txt” is used to store our ending result from our Stream Reader.

Dim req As WebRequest = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + Url)
Dim rsp As WebResponse = req.GetResponse()
Dim txt As String

Now lets actually read our request and store the result in our txt variable. Also, let’s go ahead and Return our result to the user.

Using rdr As StreamReader = New StreamReader(rsp.GetResponseStream())
          txt = rdr.ReadToEnd()
End Using
Return txt

Now, let’s put it all together and we have this. Pass the URL parameter and recieve the TinyURL in return.

    Public Function GetTinyUrl(ByVal URL As String) As String
        Try
            If Url.Length <= 30 Then
                Return URL
            End If
            If Not Url.ToLower().StartsWith("http") AndAlso Not Url.ToLower().StartsWith("ftp") Then
                Url = "http://" + Url
            End If
            Dim req As WebRequest = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + Url)
            Dim rsp As WebResponse = req.GetResponse()
            Dim txt As String
            Using rdr As StreamReader = New StreamReader(rsp.GetResponseStream())
                txt = rdr.ReadToEnd()
            End Using
            Return txt
        Catch
            Return Url
        End Try
    End Function

I hope this helps you out, and don’t forget to comment!

About Dremation

I'm just a computer programmer/site desinger.
This entry was posted in Programming, Tech News, tips and tagged , , , , . Bookmark the permalink.

3 Responses to TinyURL & .NET

  1. Pingback: TimesURL & .NET | Life Of A Jackass

  2. DM says:

    [...] regarding http://timesurl.at. If you want to use this service you can still the use code from [TinyURL & .NET]. All you have to do is change one line of [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>