title: Retrieve redirected URL with Java / HttpURLConnection
body: <p>Given a URL (String ref), I am attempting to retrieve the redirected URL as follows:</p>
  
  <pre><code>        HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
          con.setInstanceFollowRedirects(false);
          con.setRequestProperty("User-Agent","");
          int responseType = con.getResponseCode()/100;
          while (responseType == 1)
          {
              Thread.sleep(10);
              responseType = con.getResponseCode()/100;
          }
          if (responseType == 3)
              return con.getHeaderField("Location");
          return con.getURL().toString();
  </code></pre>
  
  <p>I am having several (conceptual and technical) problems with it:</p>
  
  <p><strong>Conceptual problem:</strong></p>
  
  <ul>
  <li>It works in most cases, but I don't quite understand how.</li>
  <li>All methods of the 'con' instance are called AFTER the connection is opened (when 'con' is instanciated).</li>
  <li>So how do they affect the actual result?</li>
  <li>How come calling 'setInstanceFollowRedirects' affects the returned value of 'getHeaderField'?</li>
  <li>Is there any point calling 'getResponseCode' over and over until the returned value is not 1xx?</li>
  <li>Bottom line, my general question here: is there another request/response sent through the connection every time one of these methods is invoked?</li>
  </ul>
  
  <p><strong>Technical problem:</strong></p>
  
  <ul>
  <li>Sometimes the response-code is 3xx, but 'getHeaderField' does not return the "final" URL.</li>
  <li>I tried calling my code with the returned value of 'getHeaderField' until the response-code was 2xx.</li>
  <li>But in most other cases where the response-code is 3xx, 'getHeaderField' DOES return the "final" URL, and if I call my code with this URL then I get an empty string.</li>
  </ul>
  
  <p>Can you please advise how to approach the two problems above in order to have a "100% proof" code for retrieving the "final" URL?</p>
  
  <p>Please ignore cases where the response-code is 4xx or 5xx (or anything else other than 1xx / 2xx / 3xx for that matter).</p>
  
  <p>Thanks</p>