浅析MetaWeblog

    昨天看dudu老大发布的一篇文章用Windows Live Writer在博客园发布博客,里面提到了MetaWeblog这个东西,可能大家都感觉比较陌生,什么是MetaWeblog,她有什么用处,怎么使用它,这一连串的问题是不是都涌上了你的心头,我下面就替大家解答一下这些问题。
    博客园是基于.Text开发的一套多用户博客系统。.Text提供两个可编程用户接口,SimpleBlogServiceMetablogAPI。SimpleBlogService是一个 .net 的 web service,而MetaBlogAPI是一个XML-RPC Service。她可以让用户远程调用Blog程序提供的接口来完成一些功能,比如发布新的文章,查看最新文章的列表等。
    我们如何利用MetaBlogAPI呢?我们这里要感谢Chuck Cook,他为我们准备了一个可以方便操作XML-RPC的类库。我们下面的内容就是用了XML-RPC类库。我们可以使用三种方法在客户端使用这个类库:
  • 定义一个声明服务器端方法的接口,然后调用XmlRpcProxyGen去产生那个代理类。
  • 手动的写一个调用服务器端方法的类(使用XML-RPC.net,和说起来一样简单)
  • 使用Joe Bork写的XmlRpcProxyCodeGen类去产生代理类。我这里使用这种方法,因为它可以自己写程序动态的产生代码

    那我们要怎么做呢,其实很简单。在XML-RPC.net的代码里面包含了一个简单的MetablogAPI的interface。我们来看一下他,确实很简单,下面展示的只是getRecentPosts方法:

  [XmlRpcMethod("metaWeblog.getRecentPosts",
Description="Retrieves a list of the most recent existing post "
+ "using the metaWeblog API. Returns the metaWeblog struct collection.")]
Post[] getRecentPosts(
string blogid,
string username,
string password,
int numberOfPosts);
  下面把它传给XmlRpcProxyCodeGen类去产生代理类:
// Create a CSharpCodeProvider, since I'd like code in C#
Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
// Ask it for the code gen interface
System.CodeDom.Compiler.ICodeGenerator codeGen = codeProvider.CreateGenerator();
// Setup some options, with a namespace and class name
Headblender.XmlRpc.XmlRpcProxyCodeGenOptions options = new
Headblender.XmlRpc.XmlRpcProxyCodeGenOptions("Community.BlogUtils.BlogApi", "MetaWeblog", false, false);
// Generate the code for the IMetaWeblog interface
string strCode = Headblender.XmlRpc.XmlRpcProxyCodeGen.CreateCode(  typeof(IMetaWeblog), codeGen, options );
// Write out the code to a file. Done!
StreamWriter tw = new StreamWriter("MetaWeblog.cs", false);
tw.Write( strCode );
tw.Close();
    这样就参生了MetaWeblog.cs这个文件,这里面方的是调用服务器方法的类,动态生成,不用自己动手写的哦。给出一点代码片段,让大家看看。
[CookComputing.XmlRpc.XmlRpcMethodAttribute("metaWeblog.getRecentPosts")]
public CookComputing.MetaWeblog.Post[] getRecentPosts(
string blogid, string username, string password, int numberOfPosts)
{
object xrtTemp = null;
CookComputing.MetaWeblog.Post[] xrtReturn = null;
object[] xrtArray = new object[] {
blogid,
username,
password,
numberOfPosts};
xrtTemp = this.Invoke("getRecentPosts", xrtArray);
xrtReturn = ((CookComputing.MetaWeblog.Post[])(xrtTemp));
return xrtReturn;
}
    我给出了调用远程方法的代理类,下面该怎么做你应该很明白吧,写个简易版的Windows Live Writer应该问题不大吧。可能有些朋友还是没有明白如何去指向博客园的MetaWeblogAPI,注意一下XML-RPC里面的[XmlRpcUrl()]你就会明白了。
另外我准备成立一个博客园桌面发布工具的小组,做一个属于博客园自己的writer。有兴趣的朋友可以联系我。
邮箱和MSN为:prolibertine@gmail.com







资源链接:
XML-RPC http://www.xml-rpc.net
XmlRpcProxyCodeGen http://www.cookcomputing.com/blog/archives/000221.html#221

posted on 2006-12-20 18:43  JesseZhao  阅读(3156)  评论(15编辑  收藏  举报

导航