Webservice如何调用
webservice调用方式:
(1)http方式调用
请求头增加Content-type:text/xml 或application/soap+xml
SOAPAction:方法名
请求body以xml字符串传递,xml格式定义
返回以xml字符串返回,xml某个字段是一个json字符串。
入参如下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dhcc="http://www.dhcc.com.cn"><soapenv:Header/><soapenv:Body><dhcc:方法名><!--Optional:--><dhcc:inputJsonStream><![CDATA[
json串...
]]></dhcc:inputJsonStream></dhcc:方法名></soapenv:Body>
</soapenv:Envelope>
curl -XPOST -H 'Content-Type:text/xml' -d '<xml></xml>' webserviceurl
curl -X POST -H 'Content-Type:application/soap+xml' -d @test.xml webserviceurl,其中xml文件以本地文件的方式传递
public static String callWebService(String webserviceurl, String method, String sendMsg, String contentType) {String retStr = "";HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();CloseableHttpClient closeableHttpClient = httpClientBuilder.build();HttpPost httpPost = new HttpPost(webserviceurl);// 设置请求和传输超时时间RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();httpPost.setConfig(requestConfig);try {httpPost.setHeader("Content-Type", contentType);httpPost.setHeader("SOAPAction", method);StringEntity data = new StringEntity(sendMsg,Charset.forName("UTF-8"));httpPost.setEntity(data);CloseableHttpResponse response = closeableHttpClient.execute(httpPost);HttpEntity httpEntity = response.getEntity();if (httpEntity != null) {retStr = EntityUtils.toString(httpEntity, "UTF-8");logger.debug("url:{} ret:{}", url, retStr);return retStr;}} catch (Exception e) {logger.error("callWebService", e);} finally {// 释放资源try {closeableHttpClient.close();} catch (IOException e) {logger.error("close", e);}}return null;}
(2)通过apache的webservice工具调用
Client client = WsClientUtil.getClient(notifyUrl);try {Object[] objects = client.invoke(method, getjson());if (objects != null) {result = (String)objects[0];log.info(result);log.info("耗时:" +(System.currentTimeMillis() - start));}} catch (Exception e) {throw new RuntimeException("WebSocket请求异常");}}
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** 获取Client*/
public class WsClientUtil {private static final JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();private static final Map<String, Client> clientMap = new ConcurrentHashMap(256);public static Client getClient(String wsUrl) {// 创建动态客户端
// JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();//根据WebServices接口地址创建clientif (clientMap.get(wsUrl) == null) {synchronized (WsClientUtil.class) {if (clientMap.get(wsUrl) == null) {Client client = clientFactory.createClient(wsUrl);HTTPConduit conduit = (HTTPConduit) client.getConduit();HTTPClientPolicy policy = new HTTPClientPolicy();policy.setAllowChunking(false);// 连接服务器超时时间 3秒policy.setConnectionTimeout(5000);// 等待服务器响应超时时间 3秒// 等待服务器响应超时时间 3秒policy.setReceiveTimeout(5000);conduit.setClient(policy);clientMap.put(wsUrl, client);return client;}}}return clientMap.get(wsUrl);}
}
但今天测试时,使用curl -X POST -H 'Content-Type:application/soap+xml' -d '' 可以通,用java客户端调用就报错。。具体为什么还不知道。