几种Web Service的调用方式

  • Axis直接调用

 public Object getResult(String username, String password) { Service service = new Service(); Call call = null; Object ret = null; try { // 设置代理,通过代理访问服务 AxisProperties.setProperty("http.proxyHost", "192.168.1.22"); AxisProperties.setProperty("http.proxyPort", "808"); call = (Call)service.createCall(); try { call.setTargetEndpointAddress(new URL(baseUrl)); } catch (MalformedURLException e) { e.printStackTrace(); } call.setOperationName(new QName(baseUrl, "ReturnWebService")); call.addParameter(new QName(baseUrl, "UserName"), XMLType.XSD_STRING, ParameterMode.IN); call.addParameter(new QName(baseUrl, "Password"), XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.SOAP_STRING); ret = call.invoke(new String[] {username, password}); } catch (Exception e) { // TODO: handle exception } return ret; }

  • Aixs生成Stub,调用服务代理方式

首先可以直接在Eclipse通过wsdl文件生成服务的Java Proxy, 然后直接通过

ServiceSoapProxy proxy = new ServiceSoapProxy();

proxy.invokMethod();

的方式来调用服务。

  • XFire方式

// enables SSL ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory(); Protocol protocol = new Protocol("https", easy, 8443); Protocol.registerProtocol("https", protocol); Service serviceModel = new ObjectServiceFactory().create(IGreetingService.class); XFireProxyFactory facotry = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); String endpoint = "https://192.168.0.2:8443/ws-security/services/GreetingService"; try { IGreetingService service = (IGreetingService) facotry.create(serviceModel, endpoint); System.out.println(service.greet("World")); } catch (MalformedURLException e) { e.printStackTrace(); }  

你可能感兴趣的:(WebServices)