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

有时我的URL会重定向到新页面,所以我想获取新页面的URL。

这是我的代码:

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网站,它不起作用。

如果我们输入URL打击:

 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 

我怎么解决这个问题?

在调用getInputStream()之后,只需在URLConnection实例上调用getUrl() 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); 

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

试试HtmlUnit :

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