關於XML-RPC
基本介紹
XML-RPC是工作在Internet上的遠程過程調用協定。一個XML-RPC訊息就是一個請求體為xml的http-post請求,被調用的方法在伺服器端執行並將執行結果以xml格式編碼後返回。Request example
Here's an example of an XML-RPC request:POST /RPC2 HTTP/1.0User-Agent: Frontier/5.1.2 (WinNT)Host: betty.userland.comContent-Type: text/xmlContent-length: 181
examples.getStateName
41
Response example
Here's an example of a response to an XML-RPC request:HTTP/1.1 200 OKConnection: closeContent-Length: 158Content-Type: text/xmlDate: Fri, 17 Jul 1998 19:55:08 GMTServer: UserLand Frontier/5.1.2-WinNT
South Dakota
XML-RPC入門程式
基本做法
以下的入門程式包括一個管理器(HelloHandler)、一個伺服器(HelloServer)、一個客戶程式(HelloClient)。首先要做的是創建用於遠程過程調用的類和方法,人們常常稱之為管理器。Xml-rpc管理器是一個方法和方法集,它接受xml-rpc請求,並對請求的內容進行解碼,再向一個類和方法發出請求。
管理器類
package xmlRpc;/**
* @author trier
*
* HelloHandler is a simple handler than can
* be registered with an XML-RPC server
*/
public class HelloHandler {
public String sayHello(String name){
return "Hello " + name;
}
}
伺服器程式將創建的管理器註冊到伺服器上,並為伺服器指明應用程式其他特定的參數。
伺服器類
package xmlRpc;/**
*
* HelloServer is a simple XML-RPC server
* that will take the HelloHandler class available
* for XML-PRC calls.
*
*/
import org.apache.xmlrpc.WebServer;
import org.apache.xmlrpc.XmlRpc;
import java.IOException;
public class HelloServer {
public static void main(String[] args){
if(args.length<1){
System.out.println("Usage: java HelloServer [port]");
System.exit(-1);
}
try{
XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
//start the server
System.out.println("Starting XML-RPC Server......");
WebServer server = new WebServer(Integer.parseInt(args[0]));
//register our handler class
server.addHandler("hello", new HelloHandler());
System.out.println("Now accepting requests......");
} catch(ClassNotFoundException e){
System.out.println("Could not locate SAX Driver");
} catch(IOException e){
System.out.println("Could not start server: "+e.getMessage());
}
}
}
客戶程式
package xmlRpc;/**
*
* HelloClient is a simple XML-RPC client
* that makes an XML-RPC request to HelloServer
*/
import java.i.IOException;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcClient;
import java.t.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
public class HelloClient {
public static void main(String[] args){
if(args.length<1){
System.out.println("Usage: java HelloClient [your name]");
System.exit(-1);
}
try{
//Use the Apache Xereces SAX Driver
XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
//Specify the server
XmlRpcClient client = new XmlRpcClient("http://localhost:8585");
//create request
Vector params = new Vector();
params.addElement(args[0]);
//make a request and print the result
String result = (String)client.execute("hello.sayHello",params);
System.out.println("Response from server: "+ result);
} catch(ClassNotFoundException e){
System.out.println("Could not locate SAX Driver");
} catch(MalformedURLException e){
System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage());
} catch(XmlRpcException e){
System.out.println("XmlRpcException :"+e.getMessage());
} catch(IOException e){
System.out.println("IOException:"+e.getMessage());
}
}
}