Following my previous article on Dynamically updating Twitter with Twitterizer and TinyURL Edward Lewis suggested I use bit.ly to shrink the URL. So here is the code to do that:
Public Shared Function MakeBitlyUrl(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 request As Net.HttpWebRequest = WebRequest.Create("http://api.bit.ly/shorten?version=2.0.1&longUrl=" + HttpUtility.UrlEncode(Url) + "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07")
Dim Response As WebResponse = request.GetResponse
Dim res = request.GetResponse()
Dim sr As StreamReader
sr = New StreamReader(Response.GetResponseStream)
Dim text As String = sr.ReadToEnd
sr.Close()
Dim indexOfBefore As Integer = text.IndexOf("shortUrl"": """) + 12
Dim indexOfAfter As Integer = text.IndexOf("""", indexOfBefore)
Dim shortURL As String = text.Substring(indexOfBefore, indexOfAfter - indexOfBefore)
Return shortURL
Catch generatedExceptionName As Exception
Return Url
End Try
End Function
Thanks also to Using bit.ly URL Shortening From ASP.NET (REST/JSON) for a great code example.
.Net
.net, twitter