java 调用 web service

Invoking web services with Java clients
http://www.ibm.com/developerworks/webservices/library/ws-javaclient/index.html#listing4


/*
 * DynamicProxy class
 * J2SE unamanaged client
 * Service lookup: JAX-RPC ServiceFactory
 * Service access: Dynamic Proxy
 */

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

public class DynamicProxy {

	public interface HelloInterface {

		char[] getGreeting(String string);

	}

	public static void main(String[] args) {
		String wsdlURL = "http://localhost:6080/Sample2WebService/services/Sample2?wsdl";
		String namespace = "http://Sample2.wsdk.ibm.com";
		String serviceName = "HelloInterfaceService";
		String portName = "Sample2";

		try {

			/* Service lookup */
			ServiceFactory serviceFactory = ServiceFactory.newInstance();
			Service sampleService = serviceFactory.createService(new URL(
					wsdlURL), new QName(namespace, serviceName));

			/* Service access */
			HelloInterface myProxy = (HelloInterface) sampleService.getPort(
					new QName(namespace, portName), HelloInterface.class);

			/* Service invocation */
			System.out.println(myProxy.getGreeting(args[0]));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自michael-paul.iteye.com/blog/1101206