title: How to get Android HttpURLConnection to handle a redirect
body: <p>I'm making an app that basically downloads an image from the URL that I define and save it to the user's device. I'm doing this with:</p>
  
  <pre><code> URL url = new URL("http://speedtest.clotheslinedigital.com");
   HttpURLConnection c = (HttpURLConnection) url.openConnection();
   c.setRequestMethod("GET");
   c.connect();
  </code></pre>
  
  <p>And then after checking that the storage is working properly and creating a blank file to save onto the device, I use a file output stream and an input stream to download the file to the device.</p>
  
  <p>For testing purposes, I've just been using a Dropbox download link to an image and it's been working perfectly. However, in production, I want to use a DNS subdomain redirect to link to the file, instead of a hardcoded link in my app so that I don't have to update the app if I change the path to the file that the app needs to download.</p>
  
  <p>For testing, I set a 301 redirect through my Godaddy DNS that redirects to the Dropbox link that I was using hardcoded for testing. The subdomain works perfectly when I enter the subdomain on my browser, but when I hardcore the subdomain URL into my app, nothing is downloaded and it just leaves behind a blank file (the one that was made after the request to be used to store the downloaded file).</p>
  
  <p>I've tried using setFollowRedirects() and setting it to true, which I believe is the default but I tried anyway, and it still fails.</p>
  
  <p>I thought maybe the issue was that the HttpURLConnection could not handle 301 response codes from the server, but I looked at the header of the Dropbox link and it 301 redirects the app a couple of times before downloading the file without issues.</p>
  
  <p>Any ideas on how to fix this?</p>