XML-RPC的基本描述

释放双眼,带上耳机,听听看~!

     XML-RPC的全称是XML Remote Procedure Call,即XML标准通用标记语言下的一个子集)远程方法调用。它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用使用http作为传输协议,XML作为传送信息的编码格式。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


1
2
3
4
5
6
7
8
9
10
1<?xml version="1.0"?>
2<methodCall>
3<methodName>examples.getStateName</methodName>
4<params>
5<param>
6<value><i4>41</i4></value>
7</param>
8</params>
9</methodCall>
10

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


1
2
3
4
5
6
7
8
9
1<?xml version="1.0"?>
2<methodResponse>
3<params>
4<param>
5<value><string>South Dakota</string></value>
6</param>
7</params>
8</methodResponse>
9

XML-RPC入门程序

基本做法

以下的入门程序包括一个管理器(HelloHandler)、一个 服务器(HelloServer)、一个客户程序(HelloClient)。

首先要做的是创建用于 远程过程调用的类和方法,人们常常称之为管理器。Xml-rpc管理器是一个方法和方法集,它接受xml-rpc请求,并对请求的内容进行解码,再向一个类和方法发出请求。

链接地址链接地址链接地址

管理器类


1
2
3
4
5
6
7
8
9
10
11
12
13
1package xmlRpc;
2/**
3* @author trier
4*
5* <b><code>HelloHandler</code></b> is a simple handler than can
6* be registered with an XML-RPC server
7*/
8public class HelloHandler {
9public String sayHello(String name){
10return "Hello " + name;
11}
12}
13

1
2
1  [服务器](http://baike.baidu.com/view/899.htm)程序将创建的管理器注册到服务器上,并为服务器指明应用程序其他特定的 [参数](http://baike.baidu.com/view/327406.htm)。  
2

服务器类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1package xmlRpc;
2/**
3*
4* <b><code>HelloServer</code></b> is a simple XML-RPC server
5* that will take the <code>HelloHandler</code> class available
6* for XML-PRC calls.
7* <o:p
8*/
9import org.apache.xmlrpc.WebServer;
10import org.apache.xmlrpc.XmlRpc;
11import java.IOException;
12public class HelloServer {
13public static void main(String[] args){
14if(args.length<1){
15System.out.println("Usage: java HelloServer [port]");
16System.exit(-1);
17}
18try{
19XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
20//start the server
21System.out.println("Starting XML-RPC Server......");
22WebServer server = new WebServer(Integer.parseInt(args[0]));
23//register our handler class
24server.addHandler("hello",new HelloHandler());
25System.out.println("Now accepting requests......");
26}catch(ClassNotFoundException e){
27System.out.println("Could not locate SAX Driver");
28}catch(IOException e){
29System.out.println("Could not start server: "+e.getMessage());
30}
31}
32}
33

客户程序


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1package xmlRpc;
2/**
3*
4* <b><code>HelloClient</code></b> is a simple XML-RPC client
5* that makes an XML-RPC request to <code>HelloServer</code>
6*/
7import java.i.IOException;
8import java.util.Vector;
9import org.apache.xmlrpc.XmlRpc;
10import org.apache.xmlrpc.XmlRpcClient;
11import java.t.MalformedURLException;
12import org.apache.xmlrpc.XmlRpcException;
13public class HelloClient {
14public static void main(String[] args){
15if(args.length<1){
16System.out.println("Usage: java HelloClient [your name]");
17System.exit(-1);
18}
19try{
20//Use the Apache Xereces SAX Driver
21XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
22//Specify the server
23XmlRpcClient client = new XmlRpcClient("http://localhost:8585");
24//create request
25Vector params = new Vector();
26params.addElement(args[0]);
27//make a request and print the result
28String result = (String)client.execute("hello.sayHello",params);
29System.out.println("Response from server: "+ result);
30}catch(ClassNotFoundException e){
31System.out.println("Could not locate SAX Driver");
32}catch(MalformedURLException e){
33System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage());
34}catch(XmlRpcException e){
35System.out.println("XmlRpcException :"+e.getMessage());
36}catch(IOException e){
37System.out.println("IOException:"+e.getMessage());
38}
39}
40}
41

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Git 版本回退

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索