再跑爬虫程序的时候突然遇到了如下报错:
org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected... at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263) at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222) at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183) at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135) at java.base/java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:243) at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:159) at java.base/java.util.zip.GZIPInputStream.read(GZIPInputStream.java:118) at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:70) at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185) at java.base/java.io.Reader.read(Reader.java:229) at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227) at org.apache.http.util.EntityUtils.toString(EntityUtils.java:308)网上查找资料如何解决这个问题。
先在GitHub上看到了这个issue:ApacheConnector could throw ConnectionClosedException when using httpclient: 4.5.1+ with chunked transfer encoding · Issue #3629 · jersey/jersey
这个issue的解法方法是把org.apache.httpcomponents升级到最新的版本。
2019-5-26: 最新版本是4.5.8:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.8</version> </dependency>我在网上找到了另外一篇博客:httpclient在获取response的entity时报异常 - 枯木fc - 博客园 但还是没有找到好的解决方法。 现在我的解决方法是把HttpClients放进try的括号中:
try (CloseableHttpClient httpclient = HttpClients.createDefault();) { } catch (Exception e) { e.printStackTrace(); }有其他解决方法的欢迎评论告诉我。