Free Information Technology Magazines and eBooks

Tuesday, September 08, 2009

How to use Bit.ly API to shorten URL in VB.NET

The use of shortened URL which was popularized by Twitter's tweets (autolinks) is now can be integrated on other applications by using APIs. Bit.ly, one of the most widely used shortening service is now offering free developer tools to programmers to integrate Bit.ly service on their apps. For this blog entry, I will show you how to use Bit.ly APIs on your application to shorten a given long URL. First, you need to sign-up for an account at bit.ly to receive your API key that looks like this "R_387dd73d526f14c196b235ef63d1b709".


Before we can start our project, please make sure you have the following.
- Bit.ly Login
- API key
- Bit.ly API url which is "http://api.bit.ly/shorten?version=2.0.1&format=xml"

On your Console project, add the following imports.

Imports System.Xml
Imports System.IO
Imports System.Net


Add the following functions.

'function that accept long URL and returns the bit.ly shortened URL
Public Function GetBitLyShortUrl(ByVal url As String) As String

Dim lreturn As String = ""

Dim doc As XmlDocument
doc = LoadXMLResponse(url)

If Not doc.DocumentElement Is Nothing Then
Dim shortenedNode As XmlNode = doc.DocumentElement.SelectSingleNode("results/nodeKeyVal/shortUrl")
If Not shortenedNode Is Nothing Then
lreturn = shortenedNode.InnerText
Else
lreturn = "Error while retrieving shortened URL"
End If
Else
lreturn = "Error while retrieving shortened URL"
End If

Return lreturn
End Function
'Load the return XML reponse
Private Function LoadXMLResponse(ByVal url As String) As XmlDocument

Dim doc As New XmlDocument

Try

' Load the XML response into an XML Document and return it.
doc.LoadXml(ReadBitLyResult(BitlyUrl + "&longUrl=" + url))
Return doc

Catch e As Exception

Return New XmlDocument()
End Try
End Function

' Fetches a result from bit.ly provided the URL submitted
Private Function ReadBitLyResult(ByVal url As String) As String
Dim client As New WebClient

Try

Using reader As New StreamReader(client.OpenRead(url))
' Read all of the response
Return reader.ReadToEnd()
reader.Close()
End Using

Catch e As Exception
Throw e
End Try

End Function

Where:
- GetBitLyShortUrl is the main function that accepts a long URL and returns a short URL from Bit.ly
- LoadXMLResponse is the function Load the XML response file to a XML document.
- ReadBitLyResult is the function that will fetch the result from Bit.ly

Lastly, copy and paste the following code on your Main function.

Sub Main()
Dim loginBitly As String = "foxtrot0911"
Dim apiKey As String = "R_387dd73d526f14c196b235ef63d1b709"
Dim urlToShorten As String = "http://www.fryan0911.com"
BitlyUrl &= "&login=" & loginBitly & "&apiKey=" & apiKey

Console.WriteLine(urlToShorten & "=>" & GetBitLyShortUrl(urlToShorten))
Console.ReadKey()

End Sub


Now run your project. The console should display similar to this screenshot.
How to use Bit.ly API to shorten URL in VB.NET

Download the complete VB.NET source code.

For more coding tips & tricks, subscribe now.

0 comments: