XML-RPC Examples

本文介绍如何使用Java实现XML-RPC客户端和服务器,通过实例展示了如何调用远程函数并处理响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML-RPC Examples

previous   next   AddThis Social Bookmark Button  


To demonstrate XML-RPC, we're going to create a server that uses Java to process XML-RPC messages, and we will create a Java client to call procedures on that server.

The Java side of the conversation uses the Apache XML Project's Apache XML-RPC, available at http://xml.apache.org/xmlrpc/

Put all the .jar files in appropriate path and let us create one client and one small XML-RPC Server using JAVA.

XML-RPC Client:

Let us write an XML-RPC client which will call a function called sum function. This function takes two parameters and returns their sum.

<table class="src" width="100%"
import java.util.*;
import org.apache.xmlrpc.*;

public class JavaClient {
 public static void main (String [] args) {
  try {

     XmlRpcClient server = new XmlRpcClient("http://localhost/RPC2"); 
     Vector params = new Vector();
     params.addElement(new Integer(17));
     params.addElement(new Integer(13));

     Object result = server.execute("sample.sum", params);

     int sum = ((Integer) result).intValue();
     System.out.println("The sum is: "+ sum);

   } catch (Exception exception) {
     System.err.println("JavaClient: " + exception);
   }
  }
}

Let us see what has happend in the above example client.

  • The Java package org.apache.xmlrpc contains classes for XML-RPC Java clients and XML-RPC server. E.g. XmlRpcClient.

  • The package java.util is necessary for the Vector class.

  • Function server.execute(...) sends the request to the server. The procedure sum(17,13) is called on the server as if it were a local procedure. The return value of a procedure call is always an Object.

  • Here "sample" denotes a handler that is defined in the server.

  • Note that all the parameters of the procedure call are always collected in a Vector.

  • The XmlRpcClient class is constructed by specifying the .web address. of the server machine followed by /RPC2.

    • localhost - means the local machine

    • You can specify an IP number instead of localhost, e.g. 194.80.215.219

    • You can specify a domain name like xyz.dyndns.org

    • You can specify a port number alongwith domain name as xyz.dyndns.org:8080. The default port is 80

  • Note that the result of the remote procedure call is always an Object it has to be casted to the appropriate type.

  • When problems occur (no connection, etc.) an Exception is thrown and has to be caught using catch statement.

Because of the above call, a client sends following message to the server. Note that this is handled by server.execute(...) internally and you have nothing to do with it.

<table class="src" width="100%"
<?xml version="1.0" encoding="ISO-8859-1"?>
<methodCall>
   <methodName>sample.sum</methodName>
   <params>
       <param>
            <value><int>17</int></value>
       </param>
       <param>
	    <value><int>13</int></value>
       </param>
   </params>
</methodCall>

XML-RPC Server:

Following is the source code of XML-RPC Server written in JAVA. This makes use of built-in class available in org.apache.xmlrpc.*<table class="src" width="100%"

import org.apache.xmlrpc.*;

public class JavaServer { 

 public Integer sum(int x, int y) {
    return new Integer(x+y);
 }

 public static void main (String [] args) {
  try {

     System.out.println("Attempting to start XML-RPC Server...");
     WebServer server = new WebServer(80);
     server.addHandler("sample", new JavaServer());
     server.start();
     System.out.println("Started successfully.");
     System.out.println("Accepting requests. (Halt program to stop.)");
   } catch (Exception exception) {
     System.err.println("JavaServer: " + exception);
   }
  }
 }

Let us see what we have done in the above example server.

  • The package org.apache.xmlrpc contains the class WebServer for a XML-RPC Server implementation.

  • The procedure sum that is called remotely is implemented as a public method in a class.

  • An instance of the same server class is then associated with a handler that is accessible by the client.

  • The server is initialised by the port number (here: 80).

  • When problems occur an Exception is thrown and has to be caught using catchstatement.

For the call mentioned in the given exmaple client, server sends following response back to the client:

<table class="src" width="100%"
<?xml version="1.0" encoding="ISO-8859-1"?>
<methodResponse>
    <params>
	<param>
		<value><int>30</int></value>
	</param>
    </params>
</methodResponse>

Now your server is ready so complile it and run it at your prompt as follows:

<table class="src" width="100%"
C:\ora\xmlrpc\java>java JavaServer
Attempting to start XML-RPC Server...
Started successfully.
Accepting requests. (Halt program to stop.)

Now to test the functionality, give a call to this server as follows:

<table class="src" width="100%"
C:\ora\xmlrpc\java>java JavaClient
30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值