木木的布劳格

  • 首页
  • 学无止尽
  • 愉悦生活
  • 中医
  • 软件
  • 单反摄影
  • 收藏的软件
  • About Me
木木的布劳格
记录生活点滴的静默博客
  1. 首页
  2. C sharp
  3. 正文

在Asp.Net中创建MetaWeblog API(转来的)

16 8 月, 2011 6112点热度 0人点赞 0条评论

概念

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, 然后添加引用到项目中。

。081109203752740

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
#regi
on 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

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Wordpress xmlrpc
最后更新:16 8 月, 2011

chat

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复
标签聚合
python CENTOS win10 序列号 PHP wm 激活 c#
广告
分类
  • C sharp
  • Delphi
  • Python
  • VPS
  • 中医
  • 伤寒论
  • 其他
  • 单反摄影
  • 学无止尽
  • 工作相关
  • 愉悦生活
  • 未分类
  • 软件

COPYRIGHT © 2023 木木的布劳格. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang