Search code examples
javaurl-shortener

Java: How to easily check if a URL was already shortened?


If I have a general url (not restricted to twitter or google) like this:

http://t.co/y4o14bI

is there an easy way to check if this url is shortened?

In the above case, I as a human can of course see that it was shortend, but is there an automatic and elegant way?


Solution

  • You could do a request to the URL, look if you get redirected and if so, assume it's a shortening service. For this you'd have to read the HTTP status codes.

    On the other hand, you could whitelist some URL shortening services (t.co, bit.ly, and so on) and assume all links to those domains are shortened.

    Drawback of the first method is that it isn't certain, some sites use redirects internally. The drawback of the second method is that you'd have to keep adding shortening services, although only a few are used widely.