疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

xmlrpc介绍和使用

Posted on 2011-11-11 14:58 疯狂 阅读(8560) 评论(1)  编辑  收藏 所属分类: webservicexmlwebapache项目
blogjava的api使用.net的XML-RPC,http://dudu.cnblogs.com/articles/411388.html
 格式为:http://www.cnblogs.com/你的博客名称/services/metablogapi.aspx(在Options » Configure 页面中可以查到)。例如我的是:
http://www.cnblogs.com/freeman1984/services/metablogapi.aspx,blogjava提供的方法客户端方法有:

       XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用。

    它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范一系列的实现

这种远程过程调用使用http作为传输协议XML作为传送信息的编码格式Xml-Rpc的定义尽可能的保持了简单,但同时能够传送、处理、返回复杂的数据结构。

XML-RPC是工作在Internet上的远程过程调用协议。一个XML-RPC消息就是一个请求体为xmlhttp-post请求被调用的方法在服务器端执行并将执行结果以xml格式编码后返回。

二、       XMLRPC的消息示例

请求的示例:即是在httppost请求中的消息格式是以xml形式组织的。

Here's an example of an XML-RPC request: 

POST /RPC2 HTTP1.0

User-Agent: Frontier5.1.2 (WinNT)

Host: betty.userland.com

Content-Type: textxml

Content-length181

<?xml version="1.0"?>

<methodCall>

   <methodName>examples.getStateName</methodName>

   <params>

      <param>

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

         </param>

      </params>

   </methodCall>

   响应的示例:

HTTP1.1 200 OK

Connection: close

Content-Length: 158

Content-Type: textxml

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

Server: UserLand Frontier5.1.2-WinNT

 

<?xml version="1.0"?>

<methodResponse>

   <params>

      <param>

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

         </param>

      </params>

   </methodResponse>

错误示例:

<?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>


我们可以使用apache提供的xmlrcp的jar包来简单介绍下使用,去apache的官网下载最新的jar包(http://www.apache.org/dyn/closer.cgi/ws/xmlrpc)。
其原理使用socket来传递xml信息。服务端启动一个socket,监听客户端请求,进行处理;
1 测试主类:
public class XmlRpcTest {

    
private static int port  = 1227;
    
private static String namespace = "blogger";
    
private static final String URL="http://localhost:"+port;
    
private WebServer webServer = null;
    
private XmlRpcClient client = null;
    
//启动 xml rcp server
    private void startXmlrpcServer() throws Exception {
           webServer
=new WebServer(port);//绑定端口,最终创建serverSocket
           XmlRpcServer xmlRpcServer=webServer.getXmlRpcServer();
           PropertyHandlerMapping phm
=new PropertyHandlerMapping();
           phm.addHandler(namespace, BlogTestHandler.
class);//添加处理请求的类,可以添加多个
           xmlRpcServer.setHandlerMapping(phm);
           webServer.start();
           System.out.println(
"xmlRpc WebServer has started !");


    }

    
//创建客户端
    private void createXmlrpcClient() throws XmlRpcException, MalformedURLException{
           XmlRpcClientConfigImpl config
=new XmlRpcClientConfigImpl();
           config.setServerURL(
new URL(URL));//创建一个客户端连接,最终创建soclet
           client=new XmlRpcClient();
           client.setConfig(config);
    }

    
//请求方法
    public Object reqeust(String requestMethod, Object params) throws Exception{
            Object result
=(Object)client.execute(namespace+"."+requestMethod,params);
            
return result;
    }

    

}

数据处理类:
public class BlogTestHandler {
       
public boolean deletePost(String blogId){
           
return true;
       }

       
public String[] getBlog(String blogTitle){
           
//maybe from db
           return new String[]{blogTitle,"这是返回的blog,标题是:"+blogTitle};
       }

       
}


测试:
 public static void main(String[] args) throws Exception {
        XmlRpcTest xmlRpcTest 
= new XmlRpcTest();
        xmlRpcTest.startXmlrpcServer();
        xmlRpcTest.createXmlrpcClient();
        Object[] object 
= (Object[]) xmlRpcTest.reqeust("getBlog""测试blog标题");
        System.out.println(Arrays.toString(object));
         xmlRpcTest.webServer.shutdown();
    }

}
最终看见返回值;

本来想测试blogjava的api结果,发现连不上,不知道为什么。


评论

# re: xmlrpc介绍和使用  回复  更多评论   

2011-11-11 21:59 by melin
xmlrpc性能很差。推荐用hessian

只有注册用户登录后才能发表评论。


网站导航: