class TinyUrlHelper { public static string ToTinyURLS(string txt) { Regex regx = new Regex("http://([//w+?//.//w+])+([a-zA-Z0-9//~//!//@//#//$//%//^//&//*//(//)_//-//=//+/////////?//.//://;//'//,]*)?", RegexOptions.IgnoreCase); MatchCollection mactches = regx.Matches(txt); foreach (Match match in mactches) { string tURL = MakeTinyUrl(match.Value); txt = txt.Replace(match.Value, tURL); } return txt; } public static string MakeTinyUrl(string Url) { try { if (Url.Length <= 12) { 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; } } }