Java 调用 webservice 接口,为什么这么慢,有没有好的优化方案,本人目前改成 http 方式发 soap 消息调用,速度依旧很慢,维持在平均 100ms 下不来。 之前用 cxf 的 JaxWsDynamicClientFactory 速度更加慢,400ms 、500ms 都出现过。
这是现在使用 httpClient 的调用代码
public static String callWebSV(String urlWsdl,String soap,String soapAction){
String result=null;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(urlWsdl);
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(soap.getBytes()));
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute( httpPost);
if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String back = EntityUtils.toString(responseEntity);
//log.info("httpClient 返回 soap:" + back);
result=parseResult(back);
log.info("httpClient 返回结果:" + result);
} else {
log.error("HttpClinet 返回状态码:" + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
log.error("调用错误:",e);
}
return result;
}