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

 

kick it on DotNetKicks.com

6 Comments to “Expand Urls with C# and LongUrlPlease”

  1. [...] Expand Urls with C# and LongUrlPlease – Ben Griswold ‘ they promote safer and productive browsing by lengthening short urls so that you don’t have to ‘ Ben wrote the .NET API that was lacking at longurlplease.com [...]

  2. 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

  3. Ben Griswold says:

    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!

  4. Trevor Oakley says:

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

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

    Is there a solution for this?

  5. Thanks for your post. I have continually seen that a majority of people are desirous to lose weight because they wish to look slim in addition to looking attractive. On the other hand, they do not continually realize that there are other benefits so that you can losing weight also. Doctors say that overweight people experience a variety of health conditions that can be instantly attributed to their own excess weight. Fortunately that people who are overweight plus suffering from various diseases can reduce the severity of the illnesses simply by losing weight. You are able to see a constant but identifiable improvement with health while even a small amount of weight loss is reached.

  6. I do not even know the way I stopped up right here, but I thought this publish used to be good I do not recognize who you might be however certainly you’re going to a well-known blogger when you are not already Cheers!

Leave a Reply

You can wrap your code with [ruby][/ruby] or [python][/python] blocks for syntax highlighting and you can use these traditional tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>