XML-RPC 之 Apache XML-RPC 实例 及规范(转帖2篇)

XML-RPC 之 Apache XML-RPC 实例 (转自:http://spaces.msn.com/lzhuacuo/blog/cns!18730E989D068035!507.entry?_c11_blogpart_blogpart=blogview&_c=blogpart#permalink)

 

XML-RPC 之 Apache XML-RPC 实例

作者:王恩建 来源:http://www.sentom.net

XML-RPC 是工作在 Internet 上的远程过程调用协议。通俗点讲,就是使用 HTTP 协议交互,交互的载体是 XML 文件。XML-RPC 具体的规范说 明请参考这里。

图片来自XML-RPC官方网站

XML-RPC 规范定义了六种数据类型,下表是这六种数据类型与 Java 的数据类型对应表。

XML-RPC Java

<i4> 或 <int> int

<boolean> boolean

<string> java.lang.String

<double> double

<dateTime.iso8601> java.util.Date

<struct> java.util.Hashtable

<array> java.util.Vector

<base64> byte[ ]

XML-RPC 规范的各种平台都有具体实现,XML-RPC 规范的 Java 实现都有好几种,这里我们选择了 Apache XML-RPC。

XML-RPC 服务端实现

先定义一个简单业务对象 MyHandler,远程客户端将调用该对象的方法,具体代码如下:

package net.sentom.xmlrpc;

public class MyHandler {

public String sayHello(String str){

return "Hello," + str;

}

}

然后定义一个 Servlet 名叫 MyXmlRpcServer,远程客户端通过 HTTP-POST 访问该 Servlet。

package net.sentom.xmlrpc;

import java.io.IOException;

import java.io.OutputStream;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.xmlrpc.XmlRpcServer;

public class MyXmlRpcServer extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

XmlRpcServer xmlrpc = new XmlRpcServer();

xmlrpc.addHandler("myHandler", new MyHandler());

byte[] result = xmlrpc.execute(request.getInputStream());

response.setContentType("text/xml");

response.setContentLength(result.length);

OutputStream out = response.getOutputStream();

out.write(result);

out.flush();

}

}

需要特别说明是:

xmlrpc.addHandler("myHandler", new MyHandler());

为了便于理解,这里可以看成普通的:

MyHandler myHandler = new MyHandler();

最后在web.xml文件中加入以下几行:

<servlet>

    <servlet-name>MyXmlRpcServer</servlet-name>

    <servlet-class>net.sentom.xmlrpc.MyXmlRpcServer</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>MyXmlRpcServer</servlet-name>

    <url-pattern>/MyXmlRpcServer</url-pattern>

</servlet-mapping>

XML-RPC 客户端实现

客户端相对简单一些,先来一个 Java 客户端实现 MyXmlRpcClient:

package net.sentom.xmlrpc;

import java.io.IOException;

import java.net.MalformedURLException;

import java.util.Vector;

import org.apache.xmlrpc.XmlRpcClient;

import org.apache.xmlrpc.XmlRpcException;

public class MyXmlRpcClient {

public static void main(String[] args) {

try {

XmlRpcClient xmlrpc = new XmlRpcClient("http://localhost:8080/XMLRPC/MyXmlRpcServer");

Vector params = new Vector();

params.addElement("Tom");

String result = (String) xmlrpc.execute("myHandler.sayHello",params);

System.out.println(result);

} catch (MalformedURLException e) {

System.out.println(e.toString());

} catch (XmlRpcException e) {

System.out.println(e.toString());

} catch (IOException e) {

e.printStackTrace();

}

}

http://localhost:8080/XMLRPC/MyXmlRpcServer 为 MyXmlRpcServer 的访问URL。

String result = (String) xmlrpc.execute("myHandler.sayHello",params);

再来一个 Python 客户端实现

import xmlrpclib

url = 'http://localhost:8080/XMLRPC/MyXmlRpcServer';

server = xmlrpclib.Server(url);

print server.myHandler.sayHello('Tom');

 

 

 

添加评论

9:12  |  固定链接 | 引用通告 (0) | 记录它 | Open Source

 

 

固定链接  关闭

 

http://spaces.msn.com/lzhuacuo/blog/cns!18730E989D068035!507.entry

 

 

 

 

 

 

 

XML-RPC规范(中文版) (转自:http://spaces.msn.com/lzhuacuo/blog/cns!18730E989D068035!504.entry?_c11_blogpart_blogpart=blogview&_c=blogpart#permalink)

 

XML-RPC规范(中文版)

把这篇文章转到blog里,希望rainbowsoft能看见有朝一日给blog写支持客户端post的功能时能用上

 

Tue, Jun 15, 1999; by Dave Winer. (翻译:滴水 最后修改时间:2005-3-15 后续完善)

更新 6/30/03 DW

更新 10/16/99 DW

更新 1/21/99 DW

本规范说明的XML-RPC协议实现UserLand Frontier 5.1。

关于非技术性说明,请访问XML-RPC for Newbies。

文档提供了实现XML-RPC所需要的所有信息。

前言

XML-RPC是一种基于Internet的远程函数调用协议。

XML-RPC消息都是HTTP-POST请求。请求的主要部分的XML。服务器端执行后的返回结果同样也是XML格式。

函数调用的参数可以是scalars, numbers, strings, dates等等;也可以是混合型的记录和结构体。

Request请求样式

下面是一个XML-RPC请求的例子:

POST /RPC2 HTTP/1.0

User-Agent: Frontier/5.1.2 (WinNT)

Host: betty.userland.com

Content-Type: text/xml

Content-length: 181

<?xml version="1.0"?>

<methodCall>

   <methodName>examples.getStateName</methodName>

   <params>

      <param>

         <value><i4>41</i4></value>

         </param>

      </params>

   </methodCall>

关于请求头

第一行的URI格式不是特定的。可以为空,如果服务器只处理XML-RPC请求甚至可以只是简单的一个斜线。可是,如果服务器除了XML-RPC外还提供其他的HTTP请求,URI可以帮助我们把请求指向特定的XML-RPC服务。

User-Agent和Host项是必须的。

Content-Type的值必须是text/xml.

Content-Length必须指定,而且必须是正确的值。

有效的格式

XML-RPC具有和XML一样的有效格式,并且是一个<methodCall>结构。

<methodCall>必须包含一个值为字符型的<methodName>子元素,用来表明被调用的方法。这个字符必须符合以下规定:大小写字母、数字0-9、下划线、点、冒号和斜线。至于怎么解释这个字符串将有服务器端来决定。

例如,methodName可以是一个包含执行request请求的文件的名字,可以是数据表中列的名字,还可以是表示目录和文件结构的路径。

如果远程调用接受参数,<methodCall>就必须包含<params>子元素。<params>可以包含任意个<param>元素,每个<param>包含一个<value>子元素。

Scalar <value>s <value>

<value>值被嵌入类型标签中,支持的类型如下表:

Tag Type Example

<i4> or <int> four-byte signed integer -12

<boolean> 0 (false) or 1 (true) 1

<string> string hello world

<double> double-precision signed floating point number -12.214

<dateTime.iso8601> date/time 19980717T14:08:55

<base64> base64-encoded binary eW91IGNhbid0IHJlYWQgdGhpcyE=

如果没有指定类型,默认为字符串。

<struct>s

参数值可以是<struct>。

每个<struct>包含若干<member>,每个<member>包含一个<name>和一个<value>.

如果所示为包含两个值的<struct>

<struct>

   <member>

      <name>lowerBound</name>

      <value><i4>18</i4></value>

      </member>

   <member>

      <name>upperBound</name>

      <value><i4>139</i4></value>

      </member>

   </struct>

<struct>是可以递归使用的,任何<value>都里还可以<struct>或其他任何类型,包括后面将要说明的<array>。

<array>s

值可以个<array>

一个<array>简单的有一个<data>元素。<data>可以是任何合法类型。

下面是一个有4个值的array:

<array>

   <data>

      <value><i4>12</i4></value>

      <value><string>Egypt</string></value>

      <value><boolean>0</boolean></value>

      <value><i4>-31</i4></value>

      </data>

   </array>

<array> elements do not have names.

<array> 元素没有名字。

你可以混合使用上面列出的几种类型。

<arrays>可以递归使用,其值可以是<array>或其他类型,包括上面说明的<strut>。

Response应答样式

下面是一个 XML-RPC请求:

HTTP/1.1 200 OK

Connection: close

Content-Length: 158

Content-Type: text/xml

Date: Fri, 17 Jul 1998 19:55:08 GMT

Server: UserLand Frontier/5.1.2-WinNT

<?xml version="1.0"?>

<methodResponse>

   <params>

      <param>

         <value><string>South Dakota</string></value>

         </param>

      </params>

   </methodResponse>

Respnse应答格式

除非底层操作出现错,否则总是返回200 OK.

Content-Type是text/xml。必须设置Content-Length,并且必须是正确的值。

应到内容是一个简单的XML,可是是<methodResponse>包含一个<params>,<params>包含一个<param>,<param>包含一个<value>。

<methodResponse>可能含有一个<fault>标签。<fault>的值为<struct>类型,<struct>有两个元素,值为<int>的<faultCode>和值为<string>的<faultString>。

<methodResponse>不能既有<fault>又有<params>。

Fault example

HTTP/1.1 200 OK

Connection: close

Content-Length: 426

Content-Type: text/xml

Date: Fri, 17 Jul 1998 19:55:02 GMT

Server: UserLand Frontier/5.1.2-WinNT

<?xml version="1.0"?>

<methodResponse>

   <fault>

      <value>

         <struct>

            <member>

               <name>faultCode</name>

               <value><int>4</int></value>

               </member>

            <member>

               <name>faultString</name>

               <value><string>Too many parameters.</string></value>

               </member>

            </struct>

         </value>

      </fault>

   </methodResponse>

Strategies/Goals

Firewalls. The goal of this protocol is to lay a compatible foundation across different environments, no new power is provided beyond the capabilities of the CGI interface. Firewall software can watch for POSTs whose Content-Type is text/xml.

Discoverability. We wanted a clean, extensible format that's very simple. It should be possible for an HTML coder to be able to look at a file containing an XML-RPC procedure call, understand what it's doing, and be able to modify it and have it work on the first or second try.

Easy to implement. We also wanted it to be an easy to implement protocol that could quickly be adapted to run in other environments or on other operating systems.

Updated 1/21/99 DW

The following questions came up on the UserLand discussion group as XML-RPC was being implemented in Python.

The Response Format section says "The body of the response is a single XML structure, a <methodResponse>, which can contain a single <params>..." This is confusing. Can we leave out the <params>?

No you cannot leave it out if the procedure executed successfully. There are only two options, either a response contains a <params> structure or it contains a <fault> structure. That's why we used the word "can" in that sentence.

Is "boolean" a distinct data type, or can boolean values be interchanged with integers (e.g. zero=false, non-zero=true)?

Yes, boolean is a distinct data type. Some languages/environments allow for an easy coercion from zero to false and one to true, but if you mean true, send a boolean type with the value true, so your intent can't possibly be misunderstood.

What is the legal syntax (and range) for integers? How to deal with leading zeros? Is a leading plus sign allowed? How to deal with whitespace?

An integer is a 32-bit signed number. You can include a plus or minus at the beginning of a string of numeric characters. Leading zeros are collapsed. Whitespace is not permitted. Just numeric characters preceeded by a plus or minus.

What is the legal syntax (and range) for floating point values (doubles)? How is the exponent represented? How to deal with whitespace? Can infinity and "not a number" be represented?

There is no representation for infinity or negative infinity or "not a number". At this time, only decimal point notation is allowed, a plus or a minus, followed by any number of numeric characters, followed by a period and any number of numeric characters. Whitespace is not allowed. The range of allowable values is implementation-dependent, is not specified.

What characters are allowed in strings? Non-printable characters? Null characters? Can a "string" be used to hold an arbitrary chunk of binary data?

Any characters are allowed in a string except < and &, which are encoded as < and &. A string can be used to encode binary data.

Does the "struct" element keep the order of keys. Or in other words, is the struct "foo=1, bar=2" equivalent to "bar=2, foo=1" or not?

The struct element does not preserve the order of the keys. The two structs are equivalent.

Can the <fault> struct contain other members than <faultCode> and <faultString>? Is there a global list of faultCodes? (so they can be mapped to distinct exceptions for languages like Python and Java)?

A <fault> struct may not contain members other than those specified. This is true for all other structures. We believe the specification is flexible enough so that all reasonable data-transfer needs can be accomodated within the specified structures. If you believe strongly that this is not true, please post a message on the discussion group.

There is no global list of fault codes. It is up to the server implementer, or higher-level standards to specify fault codes.

What timezone should be assumed for the dateTime.iso8601 type? UTC? localtime?

Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes about timezones.

Additions

<base64> type. 1/21/99 DW.

Updated 6/30/03 DW

Removed "ASCII" from definition of string.

Changed copyright dates, below, to 1999-2003 from 1998-99.

Copyright and disclaimer

? Copyright 1998-2003 UserLand Software. All Rights Reserved.

This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and these paragraphs are included on all such copies and derivative works.

This document may not be modified in any way, such as by removing the copyright notice or references to UserLand or other organizations. Further, while these copyright restrictions apply to the written XML-RPC specification, no claim of ownership is made by UserLand to the protocol it describes. Any party may, for commercial or non-commercial purposes, implement this protocol without royalty or license fee to UserLand. The limited permissions granted herein are perpetual and will not be revoked by UserLand or its successors or assigns.

This document and the information contained herein is provided on an "AS IS" basis and USERLAND DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

 

 

 

添加评论

9:09  |  固定链接 | 引用通告 (0) | 记录它 | Open Source

 

 

固定链接  关闭

 

http://spaces.msn.com/lzhuacuo/blog/cns!18730E989D068035!506.entry

 

 

 

 

 

 

 

XML-RPC

 

XML-RPC

 

 

 

\

http://www.xmlrpc.com/

Java Technology and Web Services

http://java.sun.com/webservices/index.jsp

Web services are Web-based enterprise applications that use open, XML-based standards and transport protocols to exchange data with calling clients. Java 2 Platform, Enterprise Edition (J2EE) provides the APIs and tools you need to create and deploy interoperable Web services and clients.

Java Technology and Web Services is organized into these subcategories:

Java Web Services Developer Pack (Java WSDP)

Java API for XML-Based RPC (JAX-RPC)

Java API for XML Registries (JAXR)

Java API for XML Processing (JAXP)

Java Architecture for XML Binding (JAXB)

SOAP with Attachments API for Java (SAAJ)

XML and Web Services Security

Java API for XML-Based RPC >

http://java.sun.com/xml/downloads/jaxrpc.html#jaxrpcspec10

Java API for XML-Based RPC (JAX-RPC) Specification 2.0

 

 

 

添加评论

8:35  |  固定链接 | 引用通告 (0) | 记录它 | Open Source

 

3 thoughts on “XML-RPC 之 Apache XML-RPC 实例 及规范(转帖2篇)”

  1. 1、工程名称必须为“XMLRPC”,因为在MyXmlRpcClient.java里,XmlRpcClient xmlrpc =

            new XmlRpcClient("http://localhost:8080/XMLRPC/MyXmlRpcServer");

      是硬编码在里面的;

    2、下载commons-codec和xmlrpc两个apache的开源库并将jar文件解压至工程的lib目录中,后者需要前者处理关于:Base64, Hex, Phonetic and URLs等的编码和解码。

  2. package net.sentom.xmlrpc;

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.OutputStream;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.xmlrpc.XmlRpcServer;

    public class MyXmlRpcServer extends HttpServlet {

      public void doPost(HttpServletRequest request, HttpServletResponse response)

        throws ServletException, IOException {

        XmlRpcServer xmlrpc = new XmlRpcServer();

        xmlrpc.addHandler("myHandler", new MyHandler());

        /*

        BufferedReader in = request.getReader();

        String s, received = new String();

        while ((s = in.readLine()) != null)

          received += s + "\n";

        in.close();

        java.util.Enumeration names = request.getHeaderNames();

        while (names.hasMoreElements()) {

          String name = (String) (names.nextElement());

          String value = request.getHeader(name);

          System.out.println(

            "http header's name is: " + name + ";value is: " + value);

        }

        System.out.println("received String stream from client: " + received);

        */

        byte[] result = xmlrpc.execute(request.getInputStream());

        String returned = new String(result);

        System.out.println("returned String stream to client: " + returned);

        response.setContentType("text/xml");

        response.setContentLength(result.length);

        OutputStream out = response.getOutputStream();

        out.write(result);

        out.flush();

      }

    }

    因为:request.getReader()和request.getInputStream()不能同时存在(见j2ee文档:public ServletInputStream getInputStream()

                                      throws java.io.IOExceptionRetrieves the body of the request as binary data using a ServletInputStream. Either this method or getReader() may be called to read the body, not both.

    ),所以在实验了客户端传向服务器端的代码后要注释掉它,然后测试服务器端传回客户端的xml文件。

    结果:

    http header's name is: content-length;value is: 139

    http header's name is: content-type;value is: text/xml

    http header's name is: cache-control;value is: no-cache

    http header's name is: pragma;value is: no-cache

    http header's name is: user-agent;value is: Java/1.4.2_04

    http header's name is: host;value is: localhost:8080

    http header's name is: accept;value is: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

    http header's name is: connection;value is: keep-alive

    received String stream from client: <?xml version="1.0"?><methodCall><methodName>myHandler.sayHello</methodName><params><param><value>Tom</value></param></params></methodCall>

    returned String stream to client: <?xml version="1.0"?><methodResponse><params><param><value>Hello,Tom</value></param></params></methodResponse>

  3. 顶上那篇译文不完整,这是一篇从网上转的完整的译文。贴在这里参考。

    转自:http://tech.16263.com/dispbbs.asp?n_id=6254&searchkey=

    XMLRPC Spec. 

    日期:2005-9-2  人气:6358      字体:[大 中 小] 

     

     

    概述

    XML-RPC是工作在Internet上的远程过程调用协议。

    XML-RPC是个HTTP-POST请求,请求体是XML。服务器上执行一个过程后仍返回XML格式的响应。

    过程的参数可以是标量,数字,字符串,日期等,也可以是复杂的记录和数据列表结构。

    示例请求

    POST /RPC2 HTTP/1.0

    User-Agent: Frontier/5.1.2 (WinNT)

    Host: betty.userland.com

    Content-Type: text/xml

    Content-length: 181

    <?xml version="1.0"?>

    <methodCall>

       <methodName>examples.getStateName</methodName>

       <params>

          <param>

             <value><i4>41</i4></value>

          </param>

       </params>

    </methodCall>

    对消息头的要求

    对头的第一行URI的格式没有指定,可以是空,一个斜线,如果服务器只处理XML-RPC 调用。可是如果服务器要处理的是HTTP请求的混合体,我们允许让URI指引请求到处理XML-RPC请求的代码去。(例如URI是“/RPC2”,告诉了服务器将请求引导到“RPC2”响应上去)

    User-Agent必须指定。

    Content-Type是text.xml。

    Content-Length必须正确地给出。

    被载内容的格式

    内容是XML,并有一个<methodCall>结构。

    <methodCall>必须包含一个<methodName>字节点,字符串类型,包含要调用方法的名字。该字符串只能包含标识字,英文字母(大写或小写)、数字、下划线、点号、冒号和斜线。它完全是为了让服务器从文字中译出方法名。

    例如,方法名是一个含有执行到来请求教本的文件,它可以是数据表中一个单元格的名字,或者是在目录文件结构中一个文件的路径。

    过程调用有参数,<methodCall>必须包含<params>子节点,<params>子节点中可以包含任意数量的<param>,它们中的每一个都有<value>。

    标量<value>

    <value>可以是标量,把值套入下面列表中的标记里可以表明值的类型。

    Tag                Type                                          Example

    ----------------------------------------------------------------------------------------------

    <i4> or <int>      four-byte signed integer                      -12

    <boolean>          0 (false) or 1 (true)                         1

    <string>           ASCII string                                  hello world

    <double>           double-precision signed floating point number -12.214

    <dateTime.iso8601> date/time                                     19980717T14:08:55

    <base64>           base64-encoded binary                         eW91IGNhbid0IHJlYWQgdGhpcyE=

    默认的类型是字符串。

    <struct>

    值的类型也可以是<stuct>。

    <struct>可以包含多个<member>,每个<member>包含一个<name>和一个<value>,下面是一个有两个元素的<struct>的例子:

    <struct>

       <member>

          <name>lowerBound</name>

          <value><i4>18</i4></value>

       </member>

       <member>

          <name>upperBound</name>

          <value><i4>139</i4></value>

       </member>

    </struct>

    <struct>可以递归,每个<value>里可以包含<struct>或其他类型,包括下面要介绍的<array>。

    <array>

    值的类型也可以是<array>。

    一个<array>包含一个<data>节点,它包含任意数量的<value>。下面是一个有四个元素的<array>。

    <array>

       <data>

          <value><i4>12</i4></value>

          <value><string>Egypt</string></value>

          <value><boolean>0</boolean></value>

          <value><i4>-31</i4></value>

       </data>

    </array>

       <array>元素没有名字。

       你可以将上面例子中的类型混合使用。和<struct>一样,<array>可以递归使用。

      

    响应示例

    HTTP/1.1 200 OK

    Connection: close

    Content-Length: 158

    Content-Type: text/xml

    Date: Fri, 17 Jul 1998 19:55:08 GMT

    Server: UserLand Frontier/5.1.2-WinNT

    <?xml version="1.0"?>

    <methodResponse>

       <params>

          <param>

             <value><string>South Dakota</string></value>

          </param>

       </params>

    </methodResponse>

    响应的格式

    除非发生低级错误,通常返回200 OK。

    Content-Type是text/xml,Content-Type必须正确地给出。

    响应体是单独的XML结构,含有一个<params>的<methodResponse>节点,<params>中仅有一个<param>,<param>中仅有一个<value>。

    <methodResponse>也可以包含一个<fault>,它包含一个<value>,其中有一个含有两个节点的<struct>,两个节点是<faultCode>(<int>)和<faultString>(<string>)。

    一个<methodResponse>不能既包含<fault>有包含<params>。下面是一个<fault>的例子。

    HTTP/1.1 200 OK

    Connection: close

    Content-Length: 426

    Content-Type: text/xml

    Date: Fri, 17 Jul 1998 19:55:02 GMT

    Server: UserLand Frontier/5.1.2-WinNT

    <?xml version="1.0"?>

    <methodResponse>

       <fault>

          <value>

             <struct>

                <member>

                   <name>faultCode</name>

                   <value><int>4</int></value>

                </member>

                <member>

                   <name>faultString</name>

                   <value><string>Too many parameters.</string></value>

                </member>

             </struct>

          </value>

       </fault>

    </methodResponse>

    策略、目标

    防火墙。这个协议的目标是为不同环境铺垫一个一致的基础,没有提供CGI接口一致性以外功能。防火墙系统软件可以看到Content-Type时text/xml的POST。

    可发现性。我们想要的是个简单的,简洁的和可扩展的格式。一个HTML的编者很容易从含有XML-RPC过程调用的文件中了解到它在做什么,并可以第一次或者的二次试着更改和使用它。

    易用性。我们还希望它能够成为能够在不同操作系统间转换时迅速适应并运行的系统。 

Comments are closed.