博客园是基于.Text开发的一套多用户博客系统。.Text提供两个可编程用户接口,SimpleBlogService和MetablogAPI。SimpleBlogService是一个 .net 的 web service,而MetaBlogAPI是一个XML-RPC Service。她可以让用户远程调用Blog程序提供的接口来完成一些功能,比如发布新的文章,查看最新文章的列表等。
那我们要怎么做呢,其实很简单。在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为:[email protected]
资源链接: XML-RPC http://www.xml-rpc.net XmlRpcProxyCodeGen http://www.cookcomputing.com/blog/archives/000221.html#221