爪哇,机器人,解析URL,重定向URI [英] java, android, resolve an url, get redirected uri

查看:219
本文介绍了爪哇,机器人,解析URL,重定向URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个身份验证保护的网址: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".

这篇关于爪哇,机器人,解析URL,重定向URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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