解决异常:Premature end of chunk coded message body: closing chunk expected

本文介绍了在使用HttpClient时遇到的'Premature end of chunk coded message body: closing chunk expected'异常。问题源于HTTP 1.1协议的分块传输,而EntityUtils.toByteArray()方法在读取内容时提前关闭了流。解决方案是将HTTP协议版本设置为1.0,确保消息体一次性完整传输。参考链接提供了更多相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现异常的代码块如下:

  CloseableHttpClient httpClient = getHttpClient();
        // 调用HttpClient发送Http请求,并对响应进行处理,转换成字节数组
        HttpResponse response;
        response = httpClient.execute(method);
        byte[] bytes = EntityUtils.toByteArray(response.getEntity());----出现异常的行。
       

异常翻译:Premature end of chunk coded message body: closing chunk expected

翻译如下:过早的关闭通过块编码的消息体:关闭块异常

关键点在于http传输协议1.0与1.1的区别,1.1协议的内容是分块传输,response获得实体事懒加载,一块一块的获取,但是这个EntityUtils工具类的.toByteArray方法实现如下:

 public static byte[] toByteArray(final HttpEntity entity) throws IOException {
        Args.notNull(entity, "Entity");
        final InputStream instream = entity.getContent();
        if (instream == null) {
            return null;
        }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值