title: A connection to (...) was leaked. Did you forget to close a response body?
body: <p>I'm having a warning message constantly, despite my code seems to be good. The message is:</p>
  
  <pre class="lang-none prettyprint-override"><code>WARNING: A connection to http://someurl.com was leaked. Did you forget to close a response body?
  java.lang.Throwable: response.body().close()
      at okhttp3.internal.platform.Platform.getStackTraceForCloseable(Platform.java:148)
      at okhttp3.RealCall.captureCallStackTrace(RealCall.java:89)
      at okhttp3.RealCall.execute(RealCall.java:73)
      at com.example.HTTPSClientReferenceRate.runClient(HTTPSClientReferenceRate.java:78)
      at com.example.HTTPSClientReferenceRate.main(HTTPSClientReferenceRate.java:137)
  </code></pre>
  
  <p>I'm working with Java 8. I've tried with the traditional <code>try-catch</code> and with this approach (<code>try-with-resources</code>):</p>
  
  <pre><code>boolean repeatRequest = true;
  
  while(repeatRequest) {
      Call call = client.newCall(request);
      try (Response response = call.execute()){
          if (!response.isSuccessful()) {
              log.error("Error with the response: " + response.message());
              continue;
          }
          ResponseBody body = response.body();
          if (body == null){
              log.error("Error when getting body from the response: " + response.message());
              continue;
          }
          BufferedReader br = new BufferedReader(body.charStream());
  
          //...DATA HANDLING
  
      } catch (Exception e) {
          log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
      }
  }
  </code></pre>
  
  <p>In fact, the first <strong>if</strong> line is never called, I always have an exception, so I cannot understand why the response/body is not closed by the try-with-resources block</p>
  
  <p>I've tried this option as well, but it didn't work either:</p>
  
  <pre><code>try (Response response = client.newCall(request).execute()) { ... }
  
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've reduced my code, and I'm still having the same error, this is even weirder:</p>
  
  <pre><code>boolean repeatRequest = true;
  
  while(repeatRequest) {
      Call call = client.newCall(request);
      try (Response response = call.execute()){
          //NOTHING
      } catch (Exception e) {
          log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
      }
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong>:</p>
  
  <p>I've tried with the traditional <code>try-catch</code> but I'm still having the same issue:</p>
  
  <pre><code>boolean repeatRequest = true;
  
  while(repeatRequest) {
      Call call = client.newCall(request);
      Response response = null;
      try {
          response = call.execute();
          try (ResponseBody body = response.body()) {
              //Nothing...
          }
      } catch (Exception e) {
          log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
      } finally {
          if (response != null){
              response.close();
          }
      }
  }
  </code></pre>