现在位置首页 / 开源代码 /正文

C#使用CookComputing.XmlRpcV2组件通过xmlrpc远程发布文章到Z-blog

作者: IT小兵 | 2013年8月15日| 热度:℃ | 评论: |参与:

俺的一个小网站www.360yi.net  一直在默默的工作,但是模板一直没有更换过。

最近不是捯饬Z-blog吗,就写了个程序把原来的数据从xml转到sql server上了。

原来网站的数据是采集的,用asp.net做的,比较简单插入数据库即可,但是z-blog有个文件创建机制,

你单纯的插入数据库是不会出现在首页的,除非是伪静态化,但是伪静态化 seo不大好。


通过查看z-blog的wiki文档 ,发现有一个接口叫做:Xml-Rpc。以前都没有接触过这个类型的协议接口,

z-blog api接口

就搜集了一些资料补习一下:

XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用。

它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。

这种远程过程调用使用http作为传输协议,XML作为传送信息的编码格式。

Xml-Rpc的定义尽可能的保持了简单,但同时能够传送、处理、返回复杂的数据结构。

Request example

Here's an example of an XML-RPC request:
POST /RPC2 HTTP/1.0User-Agent: Frontier/5.1.2 (WinNT)Host: betty.userland.comContent-Type: text/xmlContent-length: 181
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>

Response example

Here's an example of a response to an XML-RPC request:

HTTP/1.1 200 OKConnection: closeContent-Length: 158Content-Type: text/xmlDate:

Fri, 17 Jul 1998 19:55:08 GMTServer: UserLand Frontier/5.1.2-WinNT

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>South Dakota</string></value>
</param>
</params>
</methodResponse>



貌似就是request xml结构数据上来,返回xml数据回去。只是数据格式约定好了而已。不是很复杂的东西。

C#调用起来很简单,超简单,不费吹灰之力就可以完成。在观看高潮的代码之前的准备一定的前戏是不可或缺的。

  1. C# XML-RPC 开源控件:

    首先下载XML-RPC.NET,  然后添加引用到项目中。


  2. 定义协议实体类
 


  #region 微软MSN网站 使用的 MetaWeblog API.

    /// 这个结构代表用户的博客基本信息 

    /// </summary> 

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct UserBlog

    {

        public string url;

        public string blogid;

        public string blogName;

    }



    /// <summary>  

    /// 这个结构代表用户信息 

    /// </summary>  

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct UserInfo

    {

        public string url;

        public string blogid;

        public string blogName;

        public string firstname;

        public string lastname;

        public string email;

        public string nickname;

    }



    /// <summary>  

    /// 这个结构代表博客分类信息 

    /// 这后面的getCategories()方法会取到CATEGORY数据。 

    /// </summary>  

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Category

    {

        public string description;

        public string title;

    }

//    struct的含意: 


//在newPost和editPost中,struct是一个RSS 2.0规范中里面的定义。的定义如下: 


//元素

//说明

//例子 


//title

//The title of the item.

//Venice Film Festival Tries to Quit Sinking 


//link

//The URL of the item.

//http://nytimes.com/2004/12/07FEST.html 


//description

//The item synopsis.

//Some of the most heated chatter at the Venice Film Festival this week was about 

the way that the arrival of the stars at the Palazzo del Cinema was being staged. 


//author

//Email address of the author of the item 


//category

//Includes the item in one or more categories 


//comments

//URL of a page for comments relating to the item 


//enclosure

//Describes a media object that is attached to the item 


//guid

//A string that uniquely identifies the item. 


//pubDate

//Indicates when the item was published. 


//source

//The RSS channel that the item came from. 


    /// <summary>  

    /// 这个结构代表博客( 文章 )信息。 

    /// 这后面的 editPost()方法, getRecentPosts()方法 和 getPost()方法 会取倒POST数据 .  

    /// </summary>  

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Post

    {

        public string[] categories;

        public string author;

        public string link { get; set; }

        public string title;

        public string description;

        public string Content;


        public DateTime pubDate;

        public string  edtAlias { get; set; }

        public string  edtTag { get; set; } 

        public string postid;

        public string comments;


    }

    #endregion

3.定义操作类:M_MetaWeblog  继承自XmlRpcClientProtocol。需要引用:using CookComputing.XmlRpc;


 #region 网站:http://msdn.microsoft.com/en-us/library/aa905670.aspx

    ///// <summary>  

    ///// 微软MSN网站 使用的 MetaWeblog API.  

    ////  网站:http://msdn.microsoft.com/en-us/library/aa905670.aspx 

    ///// </summary>  

    public class M_MetaWeblog : XmlRpcClientProtocol

    {



        /// <summary>  

        /// Returns the most recent draft and non-draft blog posts sorted in descending order by publish date.  

        /// </summary>  

        /// <param name="blogid"> This should be the string MyBlog, 

which indicates that the post is being created in the user’s blog. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <param name="numberOfPosts"> The number of posts to return. The maximum value is 20. </param>  

        /// <returns></returns>  

        /// 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 });

        }



        /// <summary>  

        /// Posts a new entry to a blog.  

        /// </summary>  

        /// <param name="blogid"> This should be the string MyBlog, 

which indicates that the post is being created in the user’s blog. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <param name="post"> A struct representing the content to update. </param>  

        /// <param name="publish"> If false, this is a draft post. </param>  

        /// <returns> The postid of the newly-created post. </returns>  

        /// 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 });

        }


        /// <summary>  

        /// Edits an existing entry on a blog.  

        /// </summary>  

        /// <param name="postid"> The ID of the post to update. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <param name="post"> A struct representing the content to update. </param>  

        /// <param name="publish"> If false, this is a draft post. </param>  

        /// <returns> Always returns true. </returns>  

        /// 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 });

        }


        /// <summary>  

        /// Deletes a post from the blog.  

        /// </summary>  

        /// <param name="appKey"> This value is ignored. </param>  

        /// <param name="postid"> The ID of the post to update. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <param name="post"> A struct representing the content to update. </param>  

        /// <param name="publish"> This value is ignored. </param>  

        /// <returns> Always returns true. </returns>  

        /// 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 });

        }



        /// <summary>  

        /// Returns information about the user’s space. An empty array is returned if the user does not have a space.  

        /// </summary>  

        /// <param name="appKey"> This value is ignored. </param>  

        /// <param name="postid"> The ID of the post to update. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"></param> 

        /// <returns> An array of structs that represents each of the user’s blogs. The array will contain a maximum of one struct, since a user can only have a single space with a single blog. </returns>  

        /// TODO:得到用户的博客清单 

        [XmlRpcMethod("blogger.getUsersBlogs")]

        public UserBlog[] getUsersBlogs(

        string appKey,

        string username,

        string password)

        {


            return (UserBlog[])this.Invoke("getUsersBlogs", new object[] { appKey, username, password });

        }


        /// <summary>  

        /// Returns basic user info (name, e-mail, userid, and so on).  

        /// </summary>  

        /// <param name="appKey"> This value is ignored. </param>  

        /// <param name="postid"> The ID of the post to update. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"></param> 

        /// <returns> A struct containing profile information about the user.  

        /// Each struct will contain the following fields: nickname, userid, url, e-mail,  

        /// lastname, and firstname. </returns>  

        /// TODO:得到用户信息 

        [XmlRpcMethod("blogger.getUserInfo")]

        public UserInfo getUserInfo(

        string appKey,

        string username,

        string password)

        {


            return (UserInfo)this.Invoke("getUserInfo", new object[] { appKey, username, password });

        }



        /// <summary>  

        /// Returns a specific entry from a blog.  

        /// </summary>  

        /// <param name="postid"> The ID of the post to update. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <returns> Always returns true. </returns>  

        /// TODO:获取一个帖子 

        [XmlRpcMethod("metaWeblog.getPost")]

        public Post getPost(

        string postid,

        string username,

        string password)

        {


            return (Post)this.Invoke("getPost", new object[] { postid, username, password });

        }


        /// <summary>  

        /// Returns the list of categories that have been used in the blog.  

        /// </summary>  

        /// <param name="blogid"> This should be the string MyBlog, which indicates that the post is being created in the user’s blog. </param>  

        /// <param name="username"> The name of the user’s space. </param>  

        /// <param name="password"> The user’s secret word. </param>  

        /// <returns> An array of structs that contains one struct for each category. Each category struct will contain a description field that contains the name of the category. </returns>  

        /// TODO:得到博客分类 

        [XmlRpcMethod("metaWeblog.getCategories")]

        public Category[] getCategories(

        string blogid,

        string username,

        string password)

        {


            return (Category[])this.Invoke("getCategories", new object[] { blogid, username, password });

        }

    }

    #endregion

4.使用方法:

   M_MetaWeblog blog = new M_MetaWeblog();

            blog.Url = "http://localhost/zb_system/xml-rpc/index.asp";

            Post post = new Post();

            post.title = "adssdasas";

            post.categories = new string[2];

            post.categories[0] = "只可意会网";

            post.categories[1] = "只可意会网";

            post.pubDate = DateTime.Now;

            post.author = "IT";

            post.postid = "0";

            post.edtAlias = "asdasdasda" + DateTime.Now.ToString("yyyyMMddHHmmss"); ;

            post.edtTag = "{1}{2}";

            post.link = "asdasdasda" + DateTime.Now.ToString("yyyyMMddHHmmss"); ;

            post.description = "<p>人adasda</p> ";

            post.Content = "<p>人类历史上第一桩离婚,竟为了床上姿势?亚当那无脑的妻子夏娃,却是上帝安插的小三?这种种跌破眼镜的传说全部与一个女人有关,魔女莉莉丝。她敢作敢为,风风火火。有的女人是男人遗失的肋骨,有的女人却可以敲断男人的肋骨。欢迎收看【睡遍人间妖魔,计逐小三前夫】,最强悍的前妻故事。</p> ";

            string a = blog.newPost("", "用户名", "密码", post, true);


注意事项:

  1. xml-rpc协议,每个博客都不尽相同,而且也很难确定到底会返回什么东西。

  2. 我们看到z-blog的代码里只有title、categories 、description 。其他的

都不支持远程。尤其是tags不支持

2.   z-blog的xml-rpc地址在后台管理主界面有,复制即可。不要自己找你的网站路径里的,那个不一定对。

3.   找不到System.web模块的解决方法,一定要在项目中添加System.web的引用。


文档参考:http://www.fengyeju.info/134.html

http://www.cnblogs.com/domainblogs/archive/2009/03/01/1400802.html

http://wiki.rainbowsoft.org/doku.php?id=wiki:newstru


转发请注明来源:   IT分享


点击阅读本文所属分类的更多文章: 开源代码 。和高手一起交流:346717337
友荐云推荐

未注明转发、原文均为本站原创。分享本文请注明 原文链接

给您更多信息和帮助

在这里您可以找到更多:

技术交流群:346717337 Jquery插件交流

投稿:suchso@vip.qq.com

承接:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/业务咨询

抢天猫双11红包
推荐使用阿里云服务器
echarts教程系列
本月最热文章

微信扫一扫,徜徉悠嘻网,您的休闲乐园

微信公众号:快乐每一天

随机文章
标签

技术交流群:346717337

投稿:suchso@vip.qq.com

专业专注:企业网站门户/微网站/微商城/CMS系统/微信公众号运营/付费问题咨询