Here is a quick method to shorten URLs using the TinyUrl API in .net – C# and VB
C#
public static string MakeTinyUrl(string Url) { try { if (Url.Length <= 30) { return Url; } if (!Url.ToLower().StartsWith("http") && !Url.ToLower().StartsWith("ftp")) { Url = "http://" + Url; } var request = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + Url); var res = request.GetResponse(); string text; using (var reader = new StreamReader(res.GetResponseStream())) { text = reader.ReadToEnd(); } return text; } catch (Exception) { return Url; } }
VB
Public Shared Function MakeTinyUrl(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 var = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + Url) Dim res As var = request.GetResponse() Dim text As String Using reader As var = New StreamReader(res.GetResponseStream()) text = reader.ReadToEnd() End Using Return text Catch Exception ex Return Url End Try End Function
This entry was posted
on Tuesday, April 15th, 2008 at 1:24 pm and is filed under Uncategorized.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.
