Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
ptikobj Avatar asked Nov 16 '11 11:11

ptikobj


2 Answers

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.

like image 146
CodeCaster Avatar answered Sep 29 '22 12:09

CodeCaster


One signal may be to request the URL and see if it results in a redirect to another domain. However, without a good definition of what "shortened" means, there is no generic way.

like image 23
unbeli Avatar answered Sep 29 '22 12:09

unbeli