代码:
/**
* 处理跳转链接,获取重定向地址
* @param url 源地址
* @return 目标网页的绝对地址
*/
public 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();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
absUrl = location.toASCIIString();
}catch(IOException e){
e.printStackTrace();
}catch (URISyntaxException e) {
e.printStackTrace();
}finally {
try {
httpclient.close();
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return absUrl;
}