Java: how easy is it to check if a url has already been shortened?

If I have a common URL (not limited to Twitter or Google), for example:

http://t.co/y4o14bI

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

In the above case, I, as a person, can, of course, see that this was a flaw, but is there an automatic and elegant way?

+5
source share
9 answers

You can make a request to the URL, see if you redirect it, and if so, suppose that is an abbreviation. To do this, you will need to read the HTTP status codes.

Alternatively, you can use the whitelist of some URL abbreviation services (t.co, bit.ly, etc.) and assume that all links to these domains are abridged.

, , . , , .

+10

URL- , . , "", .

+2

, URL-, , :

String[] domains = {"bit.ly", "t.co"...};
for(String domain : domains){
  if(url.startsWith("http://" + domain)){
    return true;
  }
}
return false;
+1

.

, URL-.

, URL ( /), (, tinyurl) URL-, (aol.com)

.

+1

: .

:

  • www URL-.
  • (, com, edu ..) co.xx, xx .

, URL.

+1

Java, groovy ..

  • URL, ;
  • URL- HttpURLConnection
  • , 200, url , , .

- , ? , !

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!
}
+1

, , , . , , , t.co. y4o14bI CMS , .

URL- shortener .

. bit.ly , wtn.gd

http://wtn.gd/random URL-.

, HTTP HEAD- 301/302?

0

URL-, HttpCLient HTTP- HTML-. , , , .

0

URL- :

    • (.. )
    • .
    • X ( URL- )
  • HttpUrlConnection ( 301, 302)

0
source

All Articles