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 08:48

You can't.

You can only check if you list a couple of shorteners and check if the url starts with it.

You can also try checking whether the url is shorter than a given length (and contains path/query string), but some shorteners (tinyurl for example) may have longer urls than normal sites (aol.com)

I would prefer the list of known shorteners.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-28 08:55

Here's what you could do in Java, groovy and the like.

  • Get the url you want to test;
  • Open the url with HttpURLConnection
  • Check the response code
  • if it is a valid code, 200 for example, the you can retrieve the url string in long form from the connection object if it was shortened or back in its original form if it wasn't.

We all love to see some code don't we. Its crude, but hey!

String addr = "http://t.co/y4o14bI";
URL url = new URL(addr);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseCode() == 200) {
    String longUrl = connection.url;
    System.out.println(longUrl);
} else {
    // You decide what you want to do here!
}
查看更多
Evening l夕情丶
4楼-- · 2019-04-28 08:55

Actually, you as a human, can't. The only way you know that it's shortened is that it's a t.co domain. The y4o14bI could be an CMS identifier for all you know.

The best way would be to use a list of known shortener urls, and lookup against that.

And even then you would have problems. I use bit.ly with a personal domain, wtn.gd

So http://wtn.gd/random would also be a shortened URL.

You could maybe do a HTTP HEAD-request, and check for a 301/302 ?

查看更多
虎瘦雄心在
5楼-- · 2019-04-28 08:56

If you request an URL like this, your HttpCLient should receive a HTTP Redirect instead of a HTML page. This wouldn't be an evidence but at least a hint.

查看更多
趁早两清
6楼-- · 2019-04-28 08:57

if you know all the domains that can be used to shorten your URLs, check if it is contained :

String[] domains = {"bit.ly", "t.co"...};
for(String domain : domains){
  if(url.startsWith("http://" + domain)){
    return true;
  }
}
return false;
查看更多
做个烂人
7楼-- · 2019-04-28 08:59

Evaluate the URL and look for some clues:

  • the Path meets certain criteria

    • only has one step (i.e. not multiple slashes)
    • does not end with filename extensions
    • not longer than X characters (would need to evaluate various URL shortening services and adjust the upper bounds for the max token length)
  • HttpUrlConnection returns a redirect responseCode (i.e. 301, 302)

查看更多
登录 后发表回答