Expand Urls with C# and LongUrlPlease

I think everyone is familiar with those short urls which are being passed around in applications like Twitter.  It’s pretty neat that there are dozens of services willing to shorten urls for us, but what about lengthening them?  That’s where longurlplease.com comes in.  As they say, they promote safer and productive browsing by lengthening short urls so that you don’t have to.  The good folks at longurlplease provide an API documentation and demos on their site. I looked around a bit but I didn’t find a .NET/C# implementer so I put one together last night.

The included sample application merely requests that longurlplease lengthens 1+ short urls using the wrappers class which follows:

 

public class LongUrlPlease
{
    private const String LongUrlPleaseUri = "http://www.longurlplease.com/api/v1.1?{0}";

    public static Dictionary<String, String> ExpandUrls(params String[] shortUrls)
    {
        var longUrls = new Dictionary<String, String>();

        HttpWebRequest webRequest = GetLongUrlPleaseRequest(shortUrls);
        var response = (HttpWebResponse) webRequest.GetResponse();

        try
        {
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
               longUrls = 
new JavaScriptSerializer().Deserialize<Dictionary<String, String>>(sr.ReadToEnd()); } } catch (WebException ex) { } return longUrls; } private static HttpWebRequest GetLongUrlPleaseRequest(IEnumerable<string> shortUrls) { Uri longUrlPleaseUri = GetLongUrlPleaseUri(shortUrls); return (HttpWebRequest) WebRequest.Create(longUrlPleaseUri); } private static Uri GetLongUrlPleaseUri(IEnumerable<String> shortUrls) { var parameters = new StringBuilder(); String delimiter = string.Empty; foreach (var url in shortUrls) { parameters.Append(delimiter).AppendFormat("q={0}", url); delimiter = "&"; } return new Uri(string.Format(LongUrlPleaseUri, parameters), UriKind.Absolute); } }

And here are a couple sample calls:

var singleUrl = LongUrlPlease.ExpandUrls("http://tinyurl.com/87lb2n");

var multipleUrls = LongUrlPlease.ExpandUrls("http://tinyurl.com/87lb2n",
                                            "http://short.ie/cww8ag");

 

Note the code comments as I needed to use the JavaScriptSerializer rather than the DataContractJsonSerializer due to the fact that longurlplease was essentially returning anonymous types back in the response. 

Download LongUrlPlease Sample:LongUrlPlease.zip

 

Comments

  1. Hey Ben,
    Nicely done.
    One small bit of advice: The api will allow multiple short urls per request, but you need to keep it to a practical size. If there are too many there’s a risk that the request will be timed out (by appengine). For the firefox plugin, and jquery library I use 4 short urls per request.
    Cheers,
    Darragh

  2. Thanks for the comment and the good advice, Darragh. I’ll see if I can get around to limiting the api to 4 short urls (many 4 overloads with 1 – 4 string params) or parsing an unlimited list of short urls into multiple 4 url requests. Good stuff. Thanks again for the advice and for the longurlplease service. It’s great!

  3. Hi, it does not work for urls with a redirect!

    eg “http://www.reverbnation.com/c./poni/4193691″

    Is there a solution for this?

closed