Using C# to "Snip" Long URLs

There are a number of URL shortening services out these days, so it seems such a shame that developers do not more often use them to make URLs more manageable for users.

One particular service is called “Snipr” and they make it very easy to use their service with a simple soft API. Here is the code to Snip your URLs down to size:

private static string GetSniprURL(string url) {
  url = HttpUtility.UrlDecode(url);
  url = HttpUtility.HtmlDecode(url);
  url = "http://snipr.com/site/snip?r=simple&link=" + HttpUtility.UrlEncode(url);

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  request.Method = "GET";

  using (StreamReader response = new StreamReader(request.GetResponse().GetResponseStream())) {
     string responseData = response.ReadToEnd();
     return responseData.Trim().Trim('\r').Trim('\n');
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.