Java:如何轻松检查URL是否已缩短? [英] Java: How to easily check if a URL was already shortened?

查看:364
本文介绍了Java:如何轻松检查URL是否已缩短?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的一般网址(不限于推特或谷歌):

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?

在上面的例子中,我作为一个人当然可以看到它是短的,但是有一种自动和优雅的方式吗? / p>

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

推荐答案

您可以对URL进行请求,看看是否重定向,如果是,则假设它是缩短服务。为此,您必须阅读HTTP状态代码。

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.

另一方面,您可以将一些URL缩短服务列入白名单(t.co,bit.ly和等等)并假设缩短了到这些域的所有链接。

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.

这篇关于Java:如何轻松检查URL是否已缩短?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆