title: Apache httpclient: Premature end of chunk coded message body: closing chunk expected
body: <p>I use <code>httpclient-4.5.2</code> to download some pdf files, but sometimes it fails and throws a closeException. Some of the answers on google are about closing responses too early. But I checked my code carefully and didn't try to read the data after closing the response. Is there any other reason for this question?</p>
  
  <pre class="lang-java prettyprint-override"><code>org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
          at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:266)
          at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:225)
          at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:184)
          at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
          at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
          at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
          at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
          at java.io.FilterInputStream.read(FilterInputStream.java:107)
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>        HttpGet httpGet = new HttpGet(fileUrl);
          try (CloseableHttpClient httpclient1 = HttpClients.createDefault()){
              httpGet.setHeader("Cookie", cookie);
              httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
              try (CloseableHttpResponse response = httpclient1.execute(httpGet)) {
                  HttpEntity entity = response.getEntity();
                  InputStream is = entity.getContent();
                  File file = new File(tarPath);
                  if (!file.getParentFile().exists() &amp;&amp; !file.getParentFile().mkdirs()) {
                      throw new IOException("Failed to create download directory");
                  }
                  byte[] b = new byte[1024];
                  try (
                      FileOutputStream fos = new FileOutputStream(file);
                      BufferedOutputStream bos = new BufferedOutputStream(fos)
                  ) {
                      int len;
                      while ((len = is.read(b)) != -1) { // the exception is thrown from here: is.read(b)
                          bos.write(b, 0, len);
                      }
                  }
              }
         }
  </code></pre>