Java: How to easily check if a URL was already sho

2019-04-28 08:40发布

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?

9条回答
姐就是有狂的资本
2楼-- · 2019-04-28 09:04

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.

查看更多
淡お忘
3楼-- · 2019-04-28 09:05

You can't: You will have to work by assumption.

Assumption:

  • Does www exist in url.
  • Does the server name end with a valid domain (e.g. com, edu, etc.) or does it has co.xx where xx is a valid country or organization code.

And you can add more assumption based on other url shortening links.

查看更多
Viruses.
4楼-- · 2019-04-28 09:11

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.

查看更多
登录 后发表回答