Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dynamically shorting url in while sending SMS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.12k
    Comment on it

    Adding a website url within a text message is a smarter way to let your customers interact directly with your product. A website url helps customers to directly navigate to your website and use the services. But the main issue is that most website urls are very lengthy and can occupy many characters in a text, to get over to these problems we use some of the online services that provides your the felicity to reduce the length of your website url. Here is a small code segment which will help you to perform the shorting of url when using asp.net technology.


    Creating Url API variable in constructor :

    public class SmsUtil
    {
          readonly string _shortUrlAPI;
          private SmsUtil()
          {
                    _shortUrlAPI = ConfigurationManager.AppSettings["ShortUrlAPI"].ToString();
           }
    }
    

    Dynamically finding all hyperlinks in the given SMS string and replace them with short url :

     public string ConvertWithShortUrl(string source)
            {
                if(string.IsNullOrWhiteSpace(source))
                    return source;
    
                string target = source;
                var availableHyperLinks = source.Split("\t\n ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Where(s => s.StartsWith("http://") || s.StartsWith("www.") || s.StartsWith("https://"));
                foreach (string link in availableHyperLinks)
                    target = target.Replace(link, GetShortUrl(link));
    
                return target;
            }
    

    Get short url by using Google API :

    public string GetShortUrl(string url)
    {
                if (string.IsNullOrWhiteSpace(url))
                    return url;
    
                string post = "{\"longUrl\": \"" + url + "\"}";
                string shortUrl = url;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_shortUrlAPI);
                try
                {
                    request.ServicePoint.Expect100Continue = false;
                    request.Method = "POST";
                    request.ContentLength = post.Length;
                    request.ContentType = "application/json";
                    request.Headers.Add("Cache-Control", "no-cache");
    
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                        requestStream.Write(postBuffer, 0, postBuffer.Length);
                    }
    
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            using (StreamReader responseReader = new StreamReader(responseStream))
                            {
                                string json = responseReader.ReadToEnd();
                                shortUrl = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value; // Validation for filtering the returned json data
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Utils.HandleErrorMessage("Error in Short url processing", ex.StackTrace, true);
                }
                return shortUrl;
            }
    }
    

    Adding short URL Google API address with key at the end in web.config file :

    <appSettings>
    <add key="ShortUrlAPI" value="https://www.googleapis.com/urlshortener/v1/url?key=xxxxxxxx Your Key goes here xxxxxxxx" />
    </appSettings>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: