zoukankan      html  css  js  c++  java
  • 在Asp.Net中创建MetaWeblog API

    metaweblog是基于XML的RPC通信( 下载 )。这意味着你有一组预先定义的结构(简单的数据类型属性 )表示,正转客户端和服务器之间。 

    您需要使用MetaWeblog API 的以下六个结构:

    BlogInfo: 有关博客的网址, ID或名称。

    UserInfo: 博客用户的ID ,名字,姓氏或电子邮件。

    Post: 就是博客帖子,标题,正文和类别。

    CategoryInfo: 博客类别信息,编号和名称。

    MediaObject: 有关媒体对象(图像,音频和其他文件类型)的名称,类型和数据。

    MediaObjectInfo: 媒体对象。

    作为一般规则,您可以请记住, metaweblog API使用字符串类型为基本类型,参数和返回类型和不存在任何整数类型。 在几个地方也用到了布尔和Base64编码的字符串两个类型。

    MetaWeblog API有九个方法:

    metaWeblog.newPost: 增加一个新帖子。

    metaWeblog.editPost: 更新帖子。

    metaWeblog.getCategories: 获得博客的类别。

    metaWeblog.getPost: 得到一个单一的POST数据。

    metaWeblog.getRecentPosts: 得到的最近的帖子。

    metaWeblog.newMediaObject: 增加一个新的媒体对象。

    blogger.deletePost: 删除一个帖子。

    blogger.getUserInfo: 获得用户信息。

    blogger.getUsersBlogs: 得到用户的blog清单。

    如何创建metaweblog

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

    在Asp.Net中创建MetaWeblog API

    2.创建一个 HTTP Handler 或者 WebService。这里创建的是HTTP处理程序MetaWeblogAPI.ashx。并设置入口点- Class="MetaWeblogSample.MetaWeblog"。

    <%@ WebHandler Language="C#" CodeBehind="MetaWeblogAPI.ashx.cs" Class="MetaWeblogSample.MetaWeblog" %>

      3.创建结构Structures( Structs.cs ),至于如何正确创建此结构,请看 MetaWeblog API 规范。

    下面的代码是我创建的结构。 你也可以在你的项目中使用相同的代码, 因为这些结构是固定不变的。

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using CookComputing.XmlRpc;
    namespace MetaWeblogSample
    {
      #region Structs
      public struct BlogInfo
      {
        public string blogid;
        public string url;
        public string blogName;
      }
      public struct Category
      {
        public string categoryId;
        public string categoryName;
      }
      [Serializable]
      public struct CategoryInfo
      {
        public string description;
        public string htmlUrl;
        public string rssUrl;
        public string title;
        public string categoryid;
      }
      [XmlRpcMissingMapping(MappingAction.Ignore)]
      public struct Enclosure
      {
        public int length;
        public string type;
        public string url;
      }
      [XmlRpcMissingMapping(MappingAction.Ignore)]
      public struct Post
      {
        public DateTime dateCreated;
        public string description;
        public string title;
        public string[] categories;
        public string permalink;
        public object postid;
        public string userid;
        public string wp_slug;
      }
      [XmlRpcMissingMapping(MappingAction.Ignore)]
      public struct Source
      {
        public string name;
        public string url;
      }
      public struct UserInfo
      {
        public string userid;
        public string firstname;
        public string lastname;
        public string nickname;
        public string email;
        public string url;
      }
      [XmlRpcMissingMapping(MappingAction.Ignore)]
      public struct MediaObject
      {
        public string name;
        public string type;
        public byte[] bits;
      }
      [Serializable]
      public struct MediaObjectInfo
      {
        public string url;
      }
      #endregion
    }

     4.创建 MetaWeblog API 接口( IMetaWeblog.cs )。这个接口的定义也是 MetaweBlog的规范。 其中有 两组核心 MetaWeblog API 和 Blogger API。代码如下:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using CookComputing.XmlRpc;
    namespace MetaWeblogSample
    {
      public interface IMetaWeblog
      {
        #region MetaWeblog API
        [XmlRpcMethod("metaWeblog.newPost")]
        string AddPost(string blogid, string username, string password, Post post, bool publish);
        [XmlRpcMethod("metaWeblog.editPost")]
        bool UpdatePost(string postid, string username, string password, Post post, bool publish);
        [XmlRpcMethod("metaWeblog.getPost")]
        Post GetPost(string postid, string username, string password);
        [XmlRpcMethod("metaWeblog.getCategories")]
        CategoryInfo[] GetCategories(string blogid, string username, string password);
        [XmlRpcMethod("metaWeblog.getRecentPosts")]
        Post[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts);
        [XmlRpcMethod("metaWeblog.newMediaObject")]
        MediaObjectInfo NewMediaObject(string blogid, string username, string password,
          MediaObject mediaObject);
        #endregion
        #region Blogger API
        [XmlRpcMethod("blogger.deletePost")]
        [return: XmlRpcReturnValue(Description = "Returns true.")]
        bool DeletePost(string key, string postid, string username, string password, bool publish);
        [XmlRpcMethod("blogger.getUsersBlogs")]
        BlogInfo[] GetUsersBlogs(string key, string username, string password);
        [XmlRpcMethod("blogger.getUserInfo")]
        UserInfo GetUserInfo(string key, string username, string password);
        #endregion
      }
    }

    5. 也是最后一步,实现接口。。此外,你还需要一个方法来验证用

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using CookComputing.XmlRpc;
    using System.Collections.Generic;
    ///
    /// 注释说明来自网络。。
    ///
    namespace MetaWeblogSample
    {
      public class MetaWeblog : XmlRpcService, IMetaWeblog
      {
        #region Public Constructors
        public MetaWeblog()
        {
        }
        #endregion
        #region IMetaWeblog Members
        string IMetaWeblog.AddPost(string blogid, string username, string password,
          Post post, bool publish)
        {
          if (ValidateUser(username, password))
          {
            string id = string.Empty;
            // TODO: 请根据实际情况返回一个字符串,一般是Blog的ID。
            return id;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        bool IMetaWeblog.UpdatePost(string postid, string username, string password,
          Post post, bool publish)
        {
          if (ValidateUser(username, password))
          {
            bool result = false;
            // TODO: 请根据实际情况返回一个布尔值,表示是否更新成功。
            return result;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        Post IMetaWeblog.GetPost(string postid, string username, string password)
        {
          if (ValidateUser(username, password))
          {
            Post post = new Post();
            // TODO: 请根据实际情况返回一个Struct { Struct是一个规范格式,
            //    格式就是Post的属性,注意category是一个数组,是这个Post所属的类别。
            //    如果类别不存在,服务器端将只处理存在的类别}。
            return post;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        CategoryInfo[] IMetaWeblog.GetCategories(string blogid, string username, string password)
        {
          if (ValidateUser(username, password))
          {
            List<CategoryInfo> categoryInfos = new List<CategoryInfo>();
            // TODO: 请根据实际情况获取Blog的类别,并设置CategoryInfo。
            return categoryInfos.ToArray();
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        Post[] IMetaWeblog.GetRecentPosts(string blogid, string username, string password,
          int numberOfPosts)
        {
          if (ValidateUser(username, password))
          {
            List<Post> posts = new List<Post>();
            // TODO: 返回一个结构(struct)的数组(array)。
            //    每一个Struct包含getPost返回值一样的结构。请设置后返回。
            return posts.ToArray();
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        MediaObjectInfo IMetaWeblog.NewMediaObject(string blogid, string username, string password,
          MediaObject mediaObject)
        {
          if (ValidateUser(username, password))
          {
            MediaObjectInfo objectInfo = new MediaObjectInfo();
            // TODO: 返回一个数组
            //    其中blogid、username、password分别代表Blog的id(注释:如果你有两个Blog,blogid指定你需要编辑的blog)、用户名和密码。
            //    struct必须包含name, type 和bits三个元素,当然也可以包含其他元素。
            return objectInfo;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        bool IMetaWeblog.DeletePost(string key, string postid, string username, string password, bool publish)
        {
          if (ValidateUser(username, password))
          {
            bool result = false;
            // TODO: 请根据实际情况返回一个布尔值,表示是否删除成功。
            return result;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        BlogInfo[] IMetaWeblog.GetUsersBlogs(string key, string username, string password)
        {
          if (ValidateUser(username, password))
          {
            List<BlogInfo> infoList = new List<BlogInfo>();
            // TODO: 请根据实际情况获取 当前用户 Blog 信息,并设置用户 Blog 信息。
            return infoList.ToArray();
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        UserInfo IMetaWeblog.GetUserInfo(string key, string username, string password)
        {
          if (ValidateUser(username, password))
          {
            UserInfo info = new UserInfo();
            // TODO: 请根据实际情况获取 当前用户 信息,并设置用户 信息。
            return info;
          }
          throw new XmlRpcFaultException(0, "User is not valid!");
        }
        #endregion
        #region Private Methods
        private bool ValidateUser(string username, string password)
        {
          bool result = false;
          // TODO: Implement the logic to validate the user
          return result;
        }
        #endregion
      }
    }

    6.编译通过了,测试下: http://localhost:1269/MetaWeblogAPI.ashx

    在Asp.Net中创建MetaWeblog API在Asp.Net中创建MetaWeblog API

    测试是通过了,具体如何怎么用在博客上我也没做出来,还得研究。值得提醒的是 通过 http://www.xmlrpc.com 下载 的 xml-rpc.net 包包已经包含各个结构,接口和方法的代码,请自行研究。

    附件:MetaWeblogSample.zip下载地址:http://file.ddvip.com/2008_10/1224235867_ddvip_8763.zip

    ORG:http://www.sucai.com/Tech/List4/7428.htm

  • 相关阅读:
    Source InSight context 窗口丢失的解决办法
    [EffectiveC++]item41:了解隐式接口和编译器多态
    [EffectiveC++]item04:Make sure the objects are initialized before they're used
    [EffectiveC++]item3:尽可能使用const
    linux man指令问题
    解读ARM成功秘诀:薄利多销推广产品
    source insight设置问题 [问题点数:20分,结帖人leecapacity]
    totalcommander
    firefox
    处理SecureCRT中使用vim出现中文乱码问题
  • 原文地址:https://www.cnblogs.com/me115/p/1896579.html
Copyright © 2011-2022 走看看