Skip to content

Instantly share code, notes, and snippets.

@ivanursul
Created May 29, 2015 09:57
Show Gist options
  • Select an option

  • Save ivanursul/4567431acc9ddf942546 to your computer and use it in GitHub Desktop.

Select an option

Save ivanursul/4567431acc9ddf942546 to your computer and use it in GitHub Desktop.
UrlRedirectInterceptor.java
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by ivanursul on 5/29/15.
*/
public class UrlRedirectInterceptor {
public String getRedirectedUrl(String url) throws Exception {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
con.getInputStream();
if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
String redirectUrl = con.getHeaderField("Location");
return getRedirectedUrl(redirectUrl);
}
return url;
}
public static void main(String[] args) throws Exception {
UrlRedirectInterceptor interceptor = new UrlRedirectInterceptor();
System.out.println(interceptor.getRedirectedUrl("http://spreadsheets.google.com/pub?key=tFOn62dEO9QCyIKK6kgSYRQ"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment