/**
* 处理跳转链接,获取重定向地址
*
* @param url
* 源地址
* @return 目标网页的绝对地址
*/
public static String getAbsUrl(String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
String absUrl = null;
try {
response = httpclient.execute(httpget, context);
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
// System.out.println("httpget.getURI():" + httpget.getURI());
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
// System.out.println("Final HTTP location: " +
// location.toASCIIString());
absUrl = location.toASCIIString();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return absUrl;
}