【发布时间】:2015-06-09 05:15:31
【问题描述】:
我知道在 JAVA java.net.httpURLConnection 类中跟踪重定向的 URL 可能会有所帮助。因此,为此目的实现了以下方法:
public static String getRedirectedUrl(String url) throws IOException {
HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection());
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestProperty("User-Agent", "Googlebot");
con.setInstanceFollowRedirects(false);
con.connect();
String headerField = con.getHeaderField("Location");
return headerField == null ? url : headerField;
}
我的问题是此方法无法跟踪某些 URL 的重定向 URL,例如以下 URL,但是它适用于大多数重定向 URL。 http://ubuntuforums.org/search.php?do=getnew&contenttype=vBForum_Post
【问题讨论】:
-
该 URL 返回 200 状态码。它不会重定向。所以没有重定向可以跟随。
-
@JBNizet 请在浏览器中查看此 URL。它将被重定向到另一个 URL。
-
我有。两次。而且它不会重定向。
-
@JBNizet 我通过浏览器检查了重定向,似乎这个 URL 将重定向到 ubuntuforums.org/search.php?searchid=7345321
-
@JBNizet 似乎新的 url 是由服务器自动生成的
标签: java url-redirection http-redirect