title: Java HttpURLConnection response processing blocking in read
body: <p>Sends a request to the server via a <code>GET</code> request <code>HttpURLConnection</code>, the answer comes to me, sometimes <code>GZIP</code>, and sometimes not.</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.ProtocolException;
  import java.net.URL;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import java.util.zip.GZIPInputStream;
  
  public class GetResponse {
  
  
      private String getResponseFromGetRequest() {
          String requestString = "https://www";
          URL obj = null;
          try {
              obj = new URL(requestString);
          } catch (MalformedURLException e) {
              log.log(Level.WARNING, "URL obj - ", e);
              return "";
          }
          HttpURLConnection con = null;
  
          try {
              con = (HttpURLConnection) obj.openConnection();
          } catch (IOException e) {
              log.log(Level.WARNING, "Open Connection - ", e);
              return "";
          }
  
          try {
              con.setRequestMethod("GET");
          } catch (ProtocolException e) {
              log.log(Level.WARNING, "Set Request Method GET - ", e);
              return "";
          }
          con.setRequestProperty("Accept", "*/*");
  
          String contentEncoding = con.getContentEncoding();
          InputStream inputStream = null;
          if (contentEncoding !=null &amp; contentEncoding.equals("gzip")){
              try {
                  inputStream = new GZIPInputStream(con.getInputStream());
              } catch (IOException e) {
                  log.log(Level.WARNING, "Wrong connection - ", e);
                  return "";
              }
          } else {
              try {
                  inputStream = con.getInputStream();
              } catch (IOException e) {
                  log.log(Level.WARNING, "Wrong connection - ", e);
                  return "";
              }
          }
  
          BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
  
          String inputLine;
          StringBuffer response = new StringBuffer();
          try {
              while ((inputLine = in.readLine()) != null &amp; in.ready()) {
                  response.append(inputLine);
              }
          } catch (IOException e) {
              e.printStackTrace();
              log.log(Level.WARNING, "InputLine - ", e);
              return "";
          }
  
          try {
              in.close();
          } catch (IOException e) {
              log.log(Level.WARNING, "Not close BufferedReader - ", e);
              return "";
          }
          return response.toString();
      }
  }
  </code></pre>
  
  <p>Once a 100-200 requests I hang the entire program, nothing happens, no error betrays.</p>
  
  <p>Looked dupm dreads, it shows:</p>
  
  <blockquote>
    <p>"pool-55-thread-3" #630 prio=5 os_prio=0 tid=0x00007f8dc4017000 nid=0x4db3 runnable [0x00007f8d53cfa000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) at sun.security.ssl.InputRecord.read(InputRecord.java:503) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) - locked &lt;0x00000000fde85e18> (a java.lang.Object) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) - locked &lt;0x00000000fde85e28> (a java.lang.Object) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512) - locked &lt;0x00000000fde85e98> (a sun.net.www.protocol.https.DelegateHttpsURLConnection) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) - locked &lt;0x00000000fde85e98> (a sun.net.www.protocol.https.DelegateHttpsURLConnection) at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2942) at java.net.URLConnection.getContentEncoding(URLConnection.java:523) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContentEncoding(HttpsURLConnectionImpl.java:410) at GetResponse.getResponseFromGetRequest(GetResponse.java:68) at ThreadsMarathon.call(ThreadsMarathon.java:17) at ThreadsMarathon.call(ThreadsMarathon.java:7) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)</p>
  </blockquote>
  
  <p>Sometimes there appears a line 68, and sometimes 91, is in the following lines of code:</p>
  
  <blockquote>
    <p><code>String contentEncoding = con.getContentEncoding();</code>
    or
    <code>while ((inputLine = in.readLine() != null)) {</code></p>
  </blockquote>
  
  <p>Searching solution found information that the crash is due to the <code>readLine()</code>, since it is possible in some cases, the server does not send end of the symbol. Add to the condition <code>in.ready()</code>. Anyway, the problem is gone.</p>
  
  <p>On account of why the crash occurs on the line <code>String contentEncoding = con.getContentEncoding ();</code> all I'll never know, because if something is wrong with the <code>con = (HttpURLConnection) obj.openConnection ();</code> then jumped out to a message.</p>
  
  <p>Can I do something wrong? How can I avoid freezes? How to recognize that in this response will be hovering just skip it? Is it possible to install some kind of timeout that if this method is carried out more than a second - just skip it?</p>