web service调用方式
第一种静态调用web service的方法
1 try {
2 //创建webservice命名空间
3 javax.xml.namespace.QName SERVICE_NAME = new QName("http://tempuri.org/", "cc2erp");
4 //Cc2Erp,Cc2ErpSoap是用cxf框架生成的实例
5 Cc2Erp ccErp = new Cc2Erp(new URL(Constants.CON_SERVICE_URL),SERVICE_NAME);
6 Cc2ErpSoap port = ccErp.getCc2ErpSoap();
7 //tok是用来发送请求的用户名和密码
8 String tok = port.getToken("test","fesco");
9 //用来获取需要调用的json串
10 String Json = 需要获取的数据,这个方法中将数据转为json串传递;
11 //将tok和json串传给web service
12 createResult = port.createOutbandTask(tok, taskInstJson);
13 log.fine("CreateOutbandTask : "+createResult);
14
15 } catch (Exception e) {
16 e.printStackTrace();
17 createResult = false;
18 log.fine(e);
19 }
第二种通过http方式动态调用web service的方法
1 2 3 import org.apache.cxf.endpoint.Client; 4 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; 5 import org.apache.cxf.transport.http.HTTPConduit; 6 import org.apache.cxf.transports.http.configuration.ConnectionType; 7 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; 8 9 public class Utils { 10 //动态调用wsdl客户端工厂 11 private static final JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 12 //设置最大连接时间 13 private static final long CONNECTION_TIME = 30000; 14 //设置关闭 15 private static final ConnectionType CONNECTION_TYPE = ConnectionType.CLOSE; 16 //创建客户端 17 private static Client getClient(String url){ 18 return dcf.createClient(url); 19 } 20 //创建通过http连接web service 21 public static Object[] invoke(String url,String methodName,Object... params) throws Exception{ 22 Client client = getClient(url); 23 //设置HTTP连接管道 24 HTTPConduit http = (HTTPConduit) client.getConduit(); 25 //设置HTTP连接政策 26 HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); 27 httpClientPolicy.setConnectionTimeout(CONNECTION_TIME); 28 httpClientPolicy.setConnection(CONNECTION_TYPE); 29 http.setClient(httpClientPolicy); 30 //返回调用的方法名和参数 31 return client.invoke(methodName, params); 32 } 33 34 public static void main( String[] args ) 35 { 36 System.out.println(invoke("http://192.168.191.1:9001/fws-outbound-service/webservice/OutboundService?wsdl",
37 "getToken","fesco", "d31fae18a821e71fc004044d00ef4033")[0]); 38 } 39 }
第三种通过cxf动态调用web service的方法
1 /** 2 * 通过cxf框架动态调用web service 3 */ 4 public static void main(String args) throws Exception { 5 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); 6 Client client = dcf.createClient("http://localhost:9001/.../**?wsdl"); //创建客户端 7 //client. 8 Object[] res = client.invoke("getToken", "username", "userpwd"); 9 System.out.println("------------------"); 10 System.out.println( res[0] ); 11 System.out.println("------------------"); 12 13 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 14 factory.getInInterceptors().add(new LoggingInInterceptor()); 15 factory.getOutInterceptors().add(new LoggingOutInterceptor()); 16 factory.setServiceClass(HelloWorld.class); 17 factory.setAddress("http://localhost:9001/HelloWorld"); 18 HelloWorld clientHelloWorld = (HelloWorld) factory.create(); 19 String reply = clientHelloWorld.sayHi("HI"); 20 System.out.println("------------------"); 21 System.out.println(reply); 22 System.out.println("------------------"); 23 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。