第一种:
xfire:
public String test1(){ // SoapController(客户端自己定义的接口) 为服务端暴露出来的方法,方法名和参数必须和服务端的匹配 Service srvcModel = new ObjectServiceFactory() .create(SoapController.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory .newInstance().getXFire()); SoapController srvc = null; try { srvc = (SoapController) factory.create(srvcModel, "http://XXX?wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } String hello = srvc.hello(); System.out.println(hello); }
第二种:
xfire:
public String test2(){ StringWriter sw = new StringWriter(); Object[] objects = null; // Client client = new Client(new URL(interfaceWsdl)); URL url = new URL("http://XXX?wsdl"); HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection(); httpConnection.setReadTimeout(120000);//设置http连接的读超时,单位是毫秒,根据实际情况指定 httpConnection.connect(); Client client = new Client(httpConnection.getInputStream(), null); client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf( 120000));//设置发送的超时限制,单位是毫秒,根据实际情况指定 client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true"); client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true"); Object[] opAddEntryArgs = new Object[] { "", "", "" };//参数列表 try { objects = client.invoke("方法名称", opAddEntryArgs); //调用接口 } catch (Exception e) { e.printStackTrace(new PrintWriter(sw, true)); } return objects[0].toString(); }
第三种:
axis2:
public static String test3() throws Exception { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制. // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference("http://XXX");//wsdl的地址,不带wsdl options.setTo(targetEPR); // 指定sayHelloToPerson方法的参数值 Object[] opAddEntryArgs = new Object[] { "", "",""};//参数列表 // 指定方法返回值的数据类型的Class对象 Class[] classes = new Class[] { String.class }; // 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间 QName opAddEntry = new QName("http://XXX", "方法名");//xxx表示命名空间 String str = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; return str; }