如何获得的重定向URL和使用内容HttpURLConnection的(How to get redi

2019-07-20 11:43发布

有时候,我的网址会重定向到一个新的页面,所以我希望得到新的页面的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

它非常适用堆栈溢出的网站,但对于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

我怎么解决这个问题?

Answer 1:

只需调用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);


Answer 2:

其实我们可以使用HttpClient的,这是我们可以设置HttpClient.followRedirect(真)HttpClinent将处理重定向的东西。



Answer 3:

尝试的HtmlUnit :

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


文章来源: How to get redirected URL and content using HttpURLConnection