调用另外一台机器的服务时报错:
org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body
java http 调用代码
public static String doPost(String url,String jsonBody) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = “”;
try {
// 通过址默认配置创建一个httpClient实例
httpClient = HttpClients.createDefault();
HttpPost httpPost=new HttpPost(url);
httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
httpPost.setEntity(new StringEntity(jsonBody));
response = httpClient.execute(httpPost);
//System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, “UTF-8”);
response.close();
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
return result;
}
问题就出在这一行 result = EntityUtils.toString(entity, “UTF-8”);
百度上找了一天,大部分解决方式都是修改客户端 java http服务请求代码,但修改完了还是有这个问题,后面谷歌了一下,说这个问题不是由客户端造成的,是服务端的问题,服务端返回的content-length长度为100字节,但是客户端实际接收到只有90字节,所以客户端去完整读取最后10个时没有读到,没有完整读到100个,所以触发了这个错误
原因:
服务端部署的某个服务配置不正确导致的
解决方式:
公司部署修改nginx的权限后就能完全传完了
奇怪的是另外一个人在别的城市访问这个请求就是成功的,诡异!
参考链接:
https://github.com/sendgrid/sendgrid-java/issues/259
https://stackoverflow.com/questions/23152816/premature-end-of-content-length-delimited-message-body-expected
https://issues.apache.org/jira/browse/HTTPCLIENT-1352