如何使用HttpURLConnection获取重定向的URL和内容

时间:2013-02-24 22:18:07

标签: java httpurlconnection

有时我的网址会重定向到新网页,因此我想获取新网页的网址。

这是我的代码:

URL url = new URL("http://stackoverflow.com/questions/88326/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(true);

System.out.println(conn.getURL().toString());

输出结果为:

  

stackoverflow.com/questions/88326/does-elmah-handle-caught-exceptions-as-well

适用于Stack Overflow网站,但对于sears.com网站,它不起作用。

如果我们输入网址:

http://www.sears.com/search=iphone

输出仍然是:

  

http://www.sears.com/search=iphone

但实际上,该页面将重定向到:

http://www.sears.com/tvs-electronics-phones-all-cell-phones/s-1231477012?keyword=iphone&autoRedirect=true&viewItems=25&redirectType=CAT_REC_PRED

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:20)

致电getUrl()后,只需致电URLConnection个实例getInputStream()

URLConnection con = new URL(url).openConnection();
System.out.println("Orignal URL: " + con.getURL());
con.connect();
System.out.println("Connected URL: " + con.getURL());
InputStream is = con.getInputStream();
System.out.println("Redirected URL: " + con.getURL());
is.close();

如果您需要知道重定向是否在实际获取内容之前发生,以下是示例代码:

HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection());
con.setInstanceFollowRedirects(false);
con.connect();
int responseCode = con.getResponseCode();
System.out.println(responseCode);
String location = con.getHeaderField("Location");
System.out.println(location);

答案 1 :(得分:1)

实际上我们可以使用HttpClient,我们可以设置HttpClient.followRedirect(true) HttpClinent将处理重定向事件。

答案 2 :(得分:-2)

尝试HtmlUnit

final WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://www.sears.com/search=phone");
String finalUrl = page.getUrl().toString(); // the redirected url