有时我的URL会重定向到一个新页面,所以我想获取新页面的URL。如何使用HttpURLConnection获取重定向的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/tvs-electronics-phones-all-cell-phones/s-1231477012?keyword=iphone&autoRedirect=true&viewItems=25&redirectType=CAT_REC_PRED
我该如何解决这个问题?
Sears链接不是HTTP重定向:'curl - head -I http:// www.sears.com/search = iphone'。它可能通过JavaScript重定向。 – apricot 2018-02-07 22:48:38