import java.io.IOException;
import java.net.HttpURLConnection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public class Main {
public static void main(String[] args) throws IOException {
String redirectUrl = getRedrectUrl("http://w3cschool.cn");
redirectUrl = getRedrectUrl(redirectUrl);
System.out.println(redirectUrl);
}
private static String getRedrectUrl(String url) throws IOException {
Response response = Jsoup.connect(url).followRedirects(false).execute();
int status = response.statusCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
return response.header("location");
}
return null;
}
}