Premature end of chunk coded message body: closing chunk expected

今天JMeter自动化测试执行报了下面的错误:

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 org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:148)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.readResponse(HTTPSamplerBase.java:1950)

该异常 org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected 表示在读取 HTTP分块传输编码(Chunked Transfer Encoding) 的响应时,客户端期望读取到最后的结束标识(0\r\n\r\n),但服务器提前关闭了连接或未正确发送结束标识。

后面排查发现是服务超时主动断开了连接,这种情况下影响了JMeter自动化的执行和报错提示。所以修改了下脚本,处理这种情况:

// 非流式响应处理,即HTTP请求错误处理
private void errorResponse(String response) {
    log.info("进入errorResponse处理!!!");    
        // 判断是不是json格式的响应,first == { ? 是 : 不是
        response = response.replace("\n","");
        String first = response.substring(0,1);
        if("{".equals(first)) {
            // json格式的错误响应
            jsonErrorResponse(response);
        }else {
            // 非json格式的错误响应
            noJsonErrorResponse(response);
        }
}

// json格式的错误响应处理
private void jsonErrorResponse(String response) throws Exception {
    JSONObject jsonResponse = new JSONObject(response);
    String msg = jsonResponse.get("msg");

    if ("智能体不存在".equals(msg)) {
        setAssertMsg("智能体不存在",msg);
    } else {
        setAssertMsg("服务器错误",msg);
    }
}

// 非json格式的错误响应处理
private void noJsonErrorResponse(String response) throws Exception {
    String mainMag = response.substring(0,response.indexOf("at "));
    setAssertMsg("服务器/其他错误",mainMag);
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值