JAVA 怎么才能获取重定向的地址

2024-12-02 16:03:55
推荐回答(1个)
回答1:

/**
* 获取重定向地址
* @param path
* @return
* @throws Exception
*/
private String getRedirectUrl(String path) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(path)
.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setConnectTimeout(5000);
return conn.getHeaderField("Location");
}