How do I stop my HTTP URL connections from following redirects?

John Zukowski

The HttpURLConnection class has a setFollowRedirects() method that allows you to disable this feature. By default, it is enabled. However, if you call setFollowRedirects() with an argument of false, you will disable all HTTP URL connections from being redirected. If you want instance level support, you would call setInstanceFollowRedirects(). This would need to be done after getting the URLConnection from the URL, but before reading any data from it.