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 }

 第四种通过axis2框架动态调用web service的方法

 1 try{
 2     /**
 3      * 此处RPCServiceClient 对象实例建议定义成类中的static变量,
 4      * 否则多次调用会出现连接超时的错误
 5      */
 6     RPCServiceClient serviceClient = new RPCServiceClient();
 7           Options options = serviceClient.getOptions();
 8           EndpointReference targetEPR = new EndpointReference( url );
 9           options.setTo( targetEPR );//设置WS地址到终端
10           options.setTimeOutInMilliSeconds(600000L);//设置请求超时时间
11           QName opName = new QName( nameSpace, param );
12           Object[] results = serviceClient.invokeBlocking( opName, entry, returnTypes);
13           serviceClient.cleanupTransport(); 
14           serviceClient.cleanup();
15           serviceClient = null;
16           
17         return results;
18         }catch(Exception e){
19             e.printStackTrace();
20             throw new BOException(e);
21         }

 

posted on 2015-03-06 14:52  00醉酒00  阅读(974)  评论(0)    收藏  举报

导航