title: How can I identify the content-type after opening an HTTP connection?
body: <p><strong>I am opening an HTTP connection to a URL that I am generating within my program:</strong></p>
  
  <pre><code>String url = INPUT_URL;
  HttpURLConnection connection;
  while (true)
  {
      connection = (HttpURLConnection)new URL(url).openConnection();
      connection.setInstanceFollowRedirects(true);
      switch (connection.getResponseCode()/100)
      {
      case 3:
          url = connection.getHeaderField("Location");
          break;
      case 4:
      case 5:
          // Report some error
          return;
      }
  }
  </code></pre>
  
  <p><strong>Now, in <code>case 2</code> I want to identify the content-type. For example:</strong></p>
  
  <ul>
  <li><p>When connecting to <a href="http://www.google.com" rel="nofollow">http://www.google.com</a>, the content-type is Text.</p></li>
  <li><p>When connecting to <a href="http://www.google.com/favicon.ico" rel="nofollow">http://www.google.com/favicon.ico</a>, the content-type is Image.</p></li>
  </ul>
  
  <p><strong>I cannot use the URL structure in order to determine that. For example:</strong></p>
  
  <ul>
  <li>When connecting to <a href="http://www.advertising.com/favicon.ico" rel="nofollow">http://www.advertising.com/favicon.ico</a>, the response-code is 200, but the content-type is Text (the content itself is "The requested file favicon.ico was not found.").</li>
  </ul>
  
  <p><strong>I know how to read the content using an <code>InputStream</code> object, but:</strong></p>
  
  <ol>
  <li><p>I wish to avoid fetching the content itself if possible.</p></li>
  <li><p>I don't see how it can help me determine the content-type.</p></li>
  </ol>
  
  <p><strong>Any ideas would be highly appreciated... thanks.</strong></p>