public static string GetConvertedUrls(ref string message, Domain.Socioboard.Enum.UrlShortener shortnerType) { List <string> listLinks = new List <string>(); Regex urlRx = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*", RegexOptions.IgnoreCase); MatchCollection matches = urlRx.Matches(message); foreach (Match match in matches) { listLinks.Add(match.Value); } foreach (string tempLink in listLinks) { string shorturl = GetShortenUrlBit(tempLink, shortnerType); message = message.Replace(tempLink, shorturl); } return(message); }
public static string GetShortenUrlBit(string Url, Domain.Socioboard.Enum.UrlShortener shortnerType) { string url = ""; if (!Url.Contains("http")) { Url = "https://" + Url; } try { if (shortnerType == Domain.Socioboard.Enum.UrlShortener.bitlyUri) { url = "https://api-ssl.bitly.com/v3/shorten?access_token=71ec4ddc8eeb062bc8bf8583cae1fe7af81af4c7" + "&longUrl=" + Url + "&domain=bit.ly&format=json"; } else { url = "https://api-ssl.bitly.com/v3/shorten?access_token=71ec4ddc8eeb062bc8bf8583cae1fe7af81af4c7" + "&longUrl=" + Url + "&domain=j.mp&format=json"; } HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "GET"; httpRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse httResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = httResponse.GetResponseStream(); StreamReader responseStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default); string pageContent = responseStreamReader.ReadToEnd(); responseStreamReader.Close(); responseStream.Close(); httResponse.Close(); JObject JData = JObject.Parse(pageContent); if (JData["status_txt"].ToString() == "OK") { return(JData["data"]["url"].ToString()); } else { return(Url); } } catch (Exception ex) { return(Url); } }