xml-rpc 以及 xml-rpc 在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
總結
以上是生活随笔為你收集整理的xml-rpc 以及 xml-rpc 在asp.net中的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公司网上报税怎么操作 有以下六个流程
- 下一篇: 2022生源地贷款什么时候到账,一般是在