Created
May 29, 2015 09:57
-
-
Save ivanursul/4567431acc9ddf942546 to your computer and use it in GitHub Desktop.
UrlRedirectInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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