Using a URL shortener with VB .NET

              

In the first of an occasional series I thought it would be worth sharing some of the coding tips that Ive used to build this site.


The first that Ill share is how to use a URL shortening service, I chose Bit.ly, simply because it was on I'd heard of and they have a very simple API.


The reason that we are using a URL shortener is that when an article is published on twitter, you only have 140 characters for the whole tweet, meaning that the URL needs to be a short as possible. Services like bit.ly allow the transformation of a URL such as http://www.technicant.com/article/85/five-linux-distros-you-should-try can be represented simply as http://bit.ly/bkDHiK


The first thing to do, before any coding is undertaken, is to sign up to Bit.ly as youll need an API key in order to make requests.


The URL for making an API request to their shortening service is simply:

http://api.bit.ly/shorten?version=2.0.1&format=xml&login=" & loginBitly & "&apiKey=" & apiKey & "&longUrl=" & urltoShorten

for example, for the

URL : http://www.technicant.com

login : test

API Key : ABC123


the full URL would be


http://api.bit.ly/shorten?version=2.0.1&format=xml&login=test&apiKey=ABC123&longUrl=http://www.technicant.com


You can test this URL in your browser to make sure you are getting a valid response before you begin coding.

Youll notice that Ive also specified the format as xml, this is not the default response format so without specifying this, youll be unable to load the response into XML to parse.


All you then need is the following class (written in vb.net):

    
Code Block

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

Public Class clsBitly

Dim mstrBitlyUrl As String
function that accepts 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
get the shortened url using xpath
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 returned XML reponse into an XML document
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(mstrBitlyUrl & "&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 strUrl As String) As String
Dim client As New WebClient

Try

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

Catch e As Exception
Throw e
End Try

End Function

Property BitlyUrl() As String
Get
Return mstrBitlyUrl
End Get
Set(ByVal Value As String)
mstrBitlyUrl = Value
End Set
End Property



End Class


And to use it...

    
Code Block
objBitly = New clsBitly

Dim loginBitly As String = put your Bitly login here
Dim apiKey As String = put your bitly API key here
Dim urlToShorten As String = put your URL here
objBitly.BitlyUrl &= "http://api.bit.ly/shorten?version=2.0.1&format=xml&login=" & loginBitly & "&apiKey=" & apiKey

strShortURL = objBitly.GetBitLyShortUrl(urlToShorten)

objBitly = Nothing


Hopefully youll have found this brief summary on programaticaly shortening URLs useful.

Comments

Use the following form to add your comments (moderated).

Full Name:*

Email:*

Comment:*