使用Windows Live Spaces MetaWeblog API具体操作实现

MetaWeblog是基于XML的RPC下通信(一个传输协议),是现在最热门的API之一,就连微软的Windows Live Spaces 也 支持MetaWeblog API。
此文将参照 Windows Live Spaces MetaWeblog API 实现在客户端操作博客园的个人MetaWeblog API。对不起,,语句实在组织不好。。
以下定义的是操作MetaWeblog API  的基类( 片断     ),,该基类复制Windows Live Spaces MetaWeblog API:
/// TODO:得到最近发布的帖子 [XmlRpcMethod ( "metaWeblog.getRecentPosts" )] public Post[] getRecentPosts ( string blogid, string username, string password, int numberOfPosts ) { return ( Post[] )this.Invoke ( "getRecentPosts", new object[] { blogid, username, password, numberOfPosts } ); } /// TODO:增加一个最新的帖子 [XmlRpcMethod ( "metaWeblog.newPost" )] public string newPost ( string blogid, string username, string password, Post content, bool publish ) { return ( string )this.Invoke ( "newPost", new object[] { blogid, username, password, content, publish } ); } /// TODO:更新一个帖子 [XmlRpcMethod ( "metaWeblog.editPost" )] public bool editPost ( string postid, string username, string password, Post content, bool publish ) { return ( bool )this.Invoke ( "editPost", new object[] { postid, username, password, content, publish } ); } /// TODO:删除一个帖子 [XmlRpcMethod ( "blogger.deletePost" )] public bool deletePost ( string appKey, string postid, string username, string password, bool publish ) { return ( bool )this.Invoke ( "deletePost", new object[] { appKey, postid, username, password, publish } ); }
之后就可以在页面文件就可以调用 getRecentPosts (), newPost (),deletePost ()这些方法了。
<form id="form1" runat="server"> <div> <asp:Button ID="btnInsertPost" runat="server" Text="添加一个新随笔" OnClick="BtnInsertPost_Click" /> </div> <div> <asp:Button ID="btnGetListPost" runat="server" Text="得到最新的随笔" OnClick="BtnGetListPost_Click" /> </div> <div> <asp:TextBox ID="txtPostID" runat="server" ;40px" /> <asp:Button ID="btnDeletePost" runat="server" Text="删除一个随笔" OnClick="BtnDeletePost_Click" /> </div> </form>   readonly M_MetaWeblog m_blog = new M_MetaWeblog (); protected void Page_Load ( object sender, EventArgs e ) { } protected void BtnInsertPost_Click ( object sender, EventArgs e ) { m_blog.Url = "http://www.cnblogs.com/jaic-xiao/services/metaweblog.aspx"; // Post newPost = new Post (); newPost.dateCreated = DateTime.Now; newPost.title = "测试 Metablog Api 标题"; newPost.description = "测试 Metablog Api 内容"; newPost.categories = new[] { "ASP.NET", "AJAX" }; Response.Write ( m_blog.newPost ( "_blogid", "jaic-xiao", "111111", newPost, true ) ); } protected void BtnGetListPost_Click ( object sender, EventArgs e ) { m_blog.Url = "http://www.cnblogs.com/jaic-xiao/services/metaweblog.aspx"; // int i = 0; Post[] postList = m_blog.getRecentPosts ( "_blogid", "jaic-xiao", "111111", 5 ); foreach ( Post postModel in postList ) { i++; Response.Write ( string.Format("{0}-{1}.<br>", i, postModel.title) ); } } protected void BtnDeletePost_Click ( object sender, EventArgs e ) { m_blog.Url = "http://www.cnblogs.com/jaic-xiao/services/metaweblog.aspx"; // Response.Write ( m_blog.deletePost ( "_appKey", this.txtPostID.Text, "jaic-xiao", "111111", true ) ? "删除成功。" : "删除失败。" ); }
注意需要指定MetaWeblog 的Url为你的客户端API地址。完毕!
提示:当需要删除一个随笔的时候,如何找到blogId,看下图的IE地址栏中高选的部分即为BlogId.
app_img2
app_img1
Windows Live Spaces MetaWeblog API。
Windows Live Spaces MetaWeblog API Code。
XML-RPC protocol下载。
我的示例下载[ 201K ]。

你可能感兴趣的:(windows,api,live,spaces,metaweblog)