title: HttpUrlConnection Leaking Sockets with "Can't identify protocol" error message: even after closing input stream and disconnecting socket
body: <p>My application is periodically throwing Too Many Open files: Socket Exception. Lsof command shows that there are many broken sockets with "Can't identify protocol" message. So, I think somehow socket/stream are not getting closed even though I am doing clean up in final block as suggested in </p>
  
  <p><a href="https://stackoverflow.com/questions/7911840/seeing-too-many-lsof-cant-identify-protocol">Seeing too many lsof can&#39;t identify protocol</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8091984/java-app-with-urlconnection-leads-too-many-open-files">Java app with URLConnection leads &quot;Too many open files&quot;</a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>    private static Map&lt;String, List&lt;String&gt;&gt;getResponseHeaders (URL url) throws IOException {
          InputStream urlStream = null
          URLConnection urlConnection = null
  
          try{
              urlConnection = (HttpURLConnection)url.openConnection()
              urlConnection.setAllowUserInteraction(false)
              urlConnection.setRequestProperty("User-Agent", "Agent-123")
              urlConnection.setConnectTimeout(10000)
              urlConnection.setReadTimeout(15000)
              urlStream = urlConnection.inputStream
              return urlConnection.getHeaderFields()
          }catch (IOException ex){
               log.info( "Could not open the connection for url "+url +" error msg "+ex.message)
               throw ex
          }finally {
             if(urlStream){
                urlStream.close()
             }
             if(urlConnection){
                 urlConnection.disconnect()
             }
       }
  </code></pre>
  
  <p>Looking for some help here. Thanks!</p>