Get Long Url Behind Tiny Url

Few days ago I wrote a post on How to decode tiny url. After I deployed a solution for a client, i got report that some of the tiny URLs are not expanding to real long URLs. When I tried one of the short URLs in question, it turned out that short url was redirecting to a url that it was a redirect to another location. And next location was redirect to real URL. I started investigating more tiny urls especially well known micro blogging site Twitter. It turned out that twitter has become home for phishing, key loggers, click advertising links.

There are some services that have popped up that claim to expand tiny url to long url. If you base your solution based on any of these services, that would mean that you are introducing another bottle neck in your application. You can easily build this solution your self. I already showed one way in my earlier post How to decode tiny url. This has one drawback that it did not allow redirect. So you did not get to real URL if there were more than one redirects required. The solution to overcome this issue is very simple.


public static string GetLongUrl(string shortUrl)
{
 var longUrl = shortUrl;
 try
 {
  var webReq = HttpWebRequest.Create(shortUrl) as HttpWebRequest;
  webReq.AllowAutoRedirect = true;
  var webResp = webReq.GetResponse() as HttpWebResponse;
  if (webResp.StatusCode == HttpStatusCode.OK)
  {
   longUrl = webResp.ResponseUri.AbsoluteUri;	
  }
 }
 catch (Exception)
 { }
 return longUrl;
}

Search

Social

Weather

10.9 °C / 51.5 °F

weather conditions Clouds

Monthly Posts

Blog Tags