【发布时间】:2014-01-16 02:18:52
【问题描述】:
我如何获得以下 URL 的“最终位置”(又名登录页面):
我的代码(如下)将此字符串作为输入。
输出应该是类似“http://weeklyad.target.com”的东西,但我得到的只是相同的 URL。
不用说,我无法解决这个具体的案例,但我仍然需要一个通用的解决方案。
这是我的简单 Java 代码,使用 HttpURLConnection(其中 String ref 是输入):
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent","");
if (con.getResponseCode()/100 == 3)
{
String target = con.getHeaderField("Location");
if (target != null)
return target;
}
return con.getURL().toString();
有人知道我做错了什么吗?
【问题讨论】:
标签: java httpurlconnection http-redirect url-redirection