今天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);
}