爪哇,机器人,解析URL,重定向URI爪哇、机器人、重定向、URL

由网友(骚年求逆推i)分享简介:我有一个身份验证保护的网址:www.domain.com/alias I have an authentication protected url : www.domain.com/alias这要求时将返回另一个URL:www.another.com/resource.mp4(不保护)that when requ...

我有一个身份验证保护的网址:www.domain.com/alias

I have an authentication protected url : www.domain.com/alias

这要求时将返回另一个URL:www.another.com/resource.mp4(不保护)

that when requested will return another url: www.another.com/resource.mp4 (not protected)

我想知道是否存在于Java的一种方法,将从给定的一个返回真实的URL。 是这样的:第二=解析(第一)

I would like to know if exists a method in Java that will return the real url from a given one. Something like: second = resolve(first)

我想装第一,并尝试读入可能响应的位置属性,但因为我不是一个Java大师,我想知道的Java已经面临着这一点。

I'm thinking of loading the first and try to read into the response maybe the location attribute, but since I'm not a java guru I would like to know if Java already faces this.

推荐答案

这是一个问题,我曾经有过关于URL重定向。试试下面的code:

This is a problem i used to have concerning URL redirects. Try the following code:

URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();

魔术师在这里发生在这两个步骤:

The "magic" here happens in these 2 steps:

ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));

在默认情况下InstanceFollowRedirects都设置为true,但你想将它设置为false捕捉第二个URL。为了能够拿到第二个URL,从第一个URL,你需要获得所谓的位置的头字段。

By default InstanceFollowRedirects are set to true, but you want to set it to false to capture the second URL. To be able to get that second URL from the first URL, you need to get the header field called "Location".

阅读全文

相关推荐

最新文章