依赖
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.8.1</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.10.2</version></dependency>
核心代码
private static String getRealURL(String url) {String realURL = null;try {OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).build();Response response = client.newCall(request).execute();realURL = response.request().url().toString();if (realURL.equals(url)) {String body = new String(response.body().bytes());
Document document = Jsoup.parse(body);Element link = document.getElementsByClass("link").get(0);realURL = link.text();
}response.close();} catch (IOException e) {e.printStackTrace();}return realURL;}