xmlrpc简单示例

来源:互联网 发布:网络综合布线收费准则 编辑:程序博客网 时间:2024/06/06 02:46

client端:

public class JavaClient {public static void main(String[] args) throws Exception {XmlRpcClientConfigImpl conf = new XmlRpcClientConfigImpl();conf.setServerURL(new URL("http://127.0.0.1:12345"));conf.setEnabledForExtensions(true);  conf.setConnectionTimeout(60 * 1000);conf.setReplyTimeout(60 * 1000);XmlRpcClient client = new XmlRpcClient();conf.setBasicUserName("hello");conf.setBasicPassword("world");        client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));client.setConfig(conf);Vector<Object> params = new Vector<Object>();params.add(33);params.add(7);    TimingOutCallback callback = new TimingOutCallback(10 * 1000);client.executeAsync("Calculator.add", params, callback);try {Integer result = (Integer) callback.waitForResponse();System.out.println("Calculator.add = " + result);    } catch (TimeoutException e) {        System.out.println("No response from server.");    } catch (Throwable e) {    e.printStackTrace();        System.out.println("Server returned an error message.");    }}}


server端:

public class JavaServer {public static void main(String[] args) throws Exception {WebServer webServer = new WebServer(12345);XmlRpcServer server = webServer.getXmlRpcServer();PropertyHandlerMapping mapping = new PropertyHandlerMapping();//mapping.load(Thread.currentThread().getContextClassLoader(), "handlers.properties");mapping.addHandler("Calculator", Calculator.class);AuthenticationHandler handler =  new AuthenticationHandler() {@Overridepublic boolean isAuthorized(XmlRpcRequest pRequest) throws XmlRpcException { XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) pRequest.getConfig();                 return "hello".equals(config.getBasicUserName());}};mapping.setAuthenticationHandler(handler);server.setHandlerMapping(mapping);XmlRpcServerConfigImpl conf = (XmlRpcServerConfigImpl) server.getConfig();conf.setEnabledForExceptions(true);conf.setContentLengthOptional(false);webServer.start();}}
Calculator

public class Calculator {public int add(int i1, int i2) {return i1 + i2;}}



原创粉丝点击