xmlrpc 以及 xmlrpc 在asp.net中的实现

XML-RPC,是英文名称XML Remote Procedure Call的简称,即XML远程方法调用。我个人的理解是,XML是远程传输数据的形式,协议的作用是调用远程的API接口。
最初的接触是来自z-blog的一个插件。打开z-blog的官方首页,最下面有这样一句话:
安装WLWSupport插件配合使用Windows Live Writer,完美支持标签编辑,别名,摘要,上传;完全可视化写博.
这个功能就是通过XML-RPC实现的,而且是asp的实现方式。
话说回来,XML-RPC是什么神奇的东西呢?详细的介绍资料可以参考创建这个东西的官方网站XML-RPC.COM。
官方网站提供了一张图来说明这个概念:

XML-RPC在各种编程语言中都有实现,尤其是php、jsp等,当然,asp.net也有,而且还有开源的组件可用,这就是xml-rpc.net了。目前最新版本是2.4.0,使用2.0的.net framework.
虽然有了组件,新手使用起来还是有一定的难度。下面这篇教程可以帮助你快速上手:
Writing an XML-RPC server or client in ASP.Net: Part 1
Writing an XML-RPC server or client in ASP.Net: Part 2
按照上面的实例,我做了一下简化:通过XML-RPC获取网站程序的版本。
第一步,在ASP.NET中建立XML-RPC服务端。
1,把XML-RPC.NET 2.4.0的dll文件CookComputing.XmlRpcV2.dll复制到网站bin目录下。
2,创建一个ashx文件,代码如下:
view plaincopy to clipboardprint?
<!--WebHandler Language="C#" Class="xml_rpc"-->  
//页面 xml-rpc.ashx  
using System;  
using System.Web;  
using CookComputing.XmlRpc;  
[XmlRpcService(Name="ljf-xml-rpc",AutoDocumentation=true)]  
public class xml_rpc : XmlRpcService {  
    [XmlRpcMethod("showVersion")]  
    public string getVersion()  
    {  
        return "ljf xml-rpc 1.0";  
    }  

//页面 xml-rpc.ashx
using System;
using System.Web;

using CookComputing.XmlRpc;

[XmlRpcService(Name="ljf-xml-rpc",AutoDocumentation=true)]
public class xml_rpc : XmlRpcService {

    [XmlRpcMethod("showVersion")]
    public string getVersion()
    {
        return "ljf xml-rpc 1.0";
    }

}
这样服务器端就建好了。要查看服务器端有什么方法可供调用,只要打开这个页面就可以看到方法名、参数、返回值的情况。
第二步,在另外一个网站中建立XML-RPC客户端。
1、同样要把类库CookComputing.XmlRpcV2.dll拷贝到bin目录中。
2、在App_Code中建立一个类,作为XML-RPC代理。代码如下:
view plaincopy to clipboardprint?
//类xml-rpcProxy.cs  
using System.Reflection;  
using CookComputing.XmlRpc;  
/// <SUMMARY></SUMMARY>  
/// rpcProxy 的摘要说明  
///   
[XmlRpcUrl("http://www.ljf.cn/xml-rpc.ashx")]
public class rpcProxy:XmlRpcClientProtocol  
{  
    [XmlRpcMethod("showVersion")]  
    public string showVersion()  
    {  
        //  
        // TODO: 在此处添加构造函数逻辑  
        //  
        return (string)Invoke(MethodBase.GetCurrentMethod());  
    }  

//类xml-rpcProxy.cs
using System.Reflection;
using CookComputing.XmlRpc;

///
/// rpcProxy 的摘要说明
///
[XmlRpcUrl("http://www.ljf.cn/xml-rpc.ashx")]
public class rpcProxy:XmlRpcClientProtocol
{
    [XmlRpcMethod("showVersion")]
    public string showVersion()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        return (string)Invoke(MethodBase.GetCurrentMethod());
    }
}

3、在普通页面中调用XML-RPC提供的方法。这里只有一个方法,也不用传参数,代码如下:
view plaincopy to clipboardprint?
//在页面xml-rpcTest.aspx的Page_Load方法中调用XML-RPC提供的方法  
       rpcProxy p = new rpcProxy();  
       Response.Write(p.showVersion()); 
    //在页面xml-rpcTest.aspx的Page_Load方法中调用XML-RPC提供的方法
        rpcProxy p = new rpcProxy();
        Response.Write(p.showVersion());
页面打印出来的结果就是上面XML-RPC返回的字符串“ljf xml-rpc 1.0”。
更多可供参考的链接:
Meng Yan ( 孟岩 )’s Weblog:XML-RPC
IBM:用 XML-RPC 开发 Web 服务: XML-RPC 中间件
博客园XML-RPC API
XML-RPC初体验
本文来源于 龙卷风资讯网 http://www.ljf.cn/ ;原文地址:http://www.ljf.cn/2010/3/Item41584.html

原文地址:https://www.cnblogs.com/HeroBeast/p/1720749.html