MetaWeblogApi 开发, 离线写博客
在学校使用的是教育网,博客园页面打开实在太慢了,但发现使用 live writer 写博客体验还不错。就想着给我们校园博客系统支持了下 MetaWeblogApi。
实现:
1.下载rpc: CookComputing.XmlRpcV2.dll,放在bin下面
2. 接口实现:IMetaWeblogApi 参考代码
Code
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 Ncuhome.Blog.MetaWeblogApi
{
/// <summary>
/// MetaWeblog的接口和结构体
/// </summary>
#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
public interface IMetaWeblogApi
{
#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
}
}
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 Ncuhome.Blog.MetaWeblogApi
{
/// <summary>
/// MetaWeblog的接口和结构体
/// </summary>
#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
public interface IMetaWeblogApi
{
#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
}
}
Code
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;
using Ncuhome.Blog.Core;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data;
using Ncuhome.Blog.Entity;
namespace Ncuhome.Blog.MetaWeblogApi
{
public class MetaWeblogApi : XmlRpcService, IMetaWeblogApi
{
public MetaWeblogApi()
{
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="blogid"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="post"></param>
/// <param name="publish"></param>
/// <returns></returns>
string IMetaWeblogApi.AddPost(string blogid, string username, string password,
Post post, bool publish)
{
if (ValidateUser(username, password))
{
Ncuhome.Blog.Entity.Weblog_Log WL = new Ncuhome.Blog.Entity.Weblog_Log();
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
WL.Log_CateId = 1;
WL.Log_CommentCount = 0;
WL.Log_IsTop = false;
WL.Log_IsVisible = true;
WL.Log_ViewCount = 0;
WL.Log_IP = Globals.IPAddress;
WL.Log_IsDraft = false;
WL.Log_LastModiTime = DateTime.Now;
WL.Log_Title = post.title;
WL.Log_Content = post.description;
WL.Log_UserId = user.User_Id;
WL.Log_CreateTime = DateTime.Now;
WL.Log_IsPasssword = false;
WL.Log_KeyWords = post.title;
int Log_Id = 1;
Ncuhome.Blog.Core.BWeblog_log.Insert(WL,out Log_Id);
return Log_Id.ToString();
}
else
throw new XmlRpcFaultException(0, "用户验证出错!");
}
bool IMetaWeblogApi.UpdatePost(string postid, string username, string password,
Post post, bool publish)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_Log WL = new Ncuhome.Blog.Entity.Weblog_Log();
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
WL.Log_Id = Convert.ToInt32(post.postid);
WL.Log_CateId = 1;
WL.Log_CommentCount = 0;
WL.Log_IsTop = false;
WL.Log_IsVisible = true;
WL.Log_ViewCount = 0;
WL.Log_IP = Globals.IPAddress;
WL.Log_IsDraft = false;
WL.Log_LastModiTime = DateTime.Now;
WL.Log_Title = post.title;
WL.Log_Content = post.description;
WL.Log_UserId = user.User_Id;
WL.Log_CreateTime = DateTime.Now;
WL.Log_IsPasssword = false;
WL.Log_KeyWords = post.title;
Ncuhome.Blog.Core.BWeblog_log.Update(WL);
return true;
}
else
return false;
}
Post IMetaWeblogApi.GetPost(string postid, string username, string password)
{
if (ValidateUser(username, password))
{
Post post = new Post();
return post;
}
throw new XmlRpcFaultException(0, "出错了!");
}
CategoryInfo[] IMetaWeblogApi.GetCategories(string blogid, string username, string password)
{
if (ValidateUser(username, password))
{
List<CategoryInfo> categoryInfos = new List<CategoryInfo>();
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
IEnumerable<Ncuhome.Blog.Entity.Weblog_Category> ca = Ncuhome.Blog.Core.BWeblog_Category.SelectByUID(user.User_Id);
CategoryInfo haha = new CategoryInfo();
haha.categoryid = "1";
haha.description = "默认分类";
haha.htmlUrl = "";
haha.rssUrl = "";
haha.title = "默认分类";
categoryInfos.Add(haha);
foreach (var c in ca)
{
CategoryInfo cate = new CategoryInfo();
cate.categoryid = c.Cate_Id.ToString();
cate.description = c.Cate_Name;
cate.htmlUrl = "";
cate.rssUrl = "";
cate.title = c.Cate_Name;
categoryInfos.Add(cate);
}
return categoryInfos.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
Post[] IMetaWeblogApi.GetRecentPosts(string blogid, string username, string password,
int numberOfPosts)
{
if (ValidateUser(username, password))
{
List<Post> posts = new List<Post>();
int TxzId = GetTxzIdByTxzUID(username);
IEnumerable<Ncuhome.Blog.Entity.Weblog_Log> WL = BWeblog_log.GetByBlogUID(TxzId);
foreach (var w in WL)
{
Post p = new Post();
p.categories = new string[] { w.Log_CateId.ToString()};
p.dateCreated = w.Log_CreateTime;
p.description = w.Log_KeyWords;
p.postid = w.Log_Id;
p.title = w.Log_Title;
p.userid = w.Log_UserId.ToString();
posts.Add(p);
}
return posts.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
MediaObjectInfo IMetaWeblogApi.NewMediaObject(string blogid, string username, string password,
MediaObject mediaObject)
{
if (ValidateUser(username, password))
{
MediaObjectInfo objectInfo = new MediaObjectInfo();
return objectInfo;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 删除日志
/// </summary>
/// <param name="key"></param>
/// <param name="postid"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="publish"></param>
/// <returns></returns>
bool IMetaWeblogApi.DeletePost(string key, string postid, string username, string password, bool publish)
{
if (ValidateUser(username, password))
{
bool result = true;
return result;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 博客信息
/// </summary>
/// <param name="key"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
BlogInfo[] IMetaWeblogApi.GetUsersBlogs(string key, string username, string password)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
List<BlogInfo> infoList = new List<BlogInfo>();
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
BlogInfo bi = new BlogInfo();
bi.blogid = user.User_Id.ToString();
bi.blogName = user.User_NickName;
bi.url = "http://blog.ncuhome.cn/"+BlogContext.Current.Owner.User_DomainName+"/log";
infoList.Add(bi);
return infoList.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 用户信息
/// </summary>
/// <param name="key"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
UserInfo IMetaWeblogApi.GetUserInfo(string key, string username, string password)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
UserInfo info = new UserInfo();
Ncuhome.Blog.Entity.Weblog_User user = new Ncuhome.Blog.Entity.Weblog_User();
user = BWeblog_User.getByTxzID(Convert.ToInt32( TxzId));
info.email = user.User_MSN;
info.firstname = "";
info.lastname = user.User_Name;
info.nickname = user.User_NickName;
info.userid = user.User_Id.ToString();
return info;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
//========================================================================
//==================上面都是实现接口的方法,下面是我们的方法
/// <summary>
/// 用户登陆验证
/// </summary>
/// <param name="TxzId">通行证的ID</param>
/// <param name="TxzPsw">通行证的密码</param>
/// <returns>是否通过验证</returns>
private bool ValidateUser(string TxzId, string password)
{
return Ncuhome.Login.Login.CheckLogin(TxzId, password);
}
private int GetTxzIdByTxzUID(string TxzUID)
{
return BGDXSJBXX.GetTxzIdByTxzUID(TxzUID);
}
}
}
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;
using Ncuhome.Blog.Core;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data;
using Ncuhome.Blog.Entity;
namespace Ncuhome.Blog.MetaWeblogApi
{
public class MetaWeblogApi : XmlRpcService, IMetaWeblogApi
{
public MetaWeblogApi()
{
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="blogid"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="post"></param>
/// <param name="publish"></param>
/// <returns></returns>
string IMetaWeblogApi.AddPost(string blogid, string username, string password,
Post post, bool publish)
{
if (ValidateUser(username, password))
{
Ncuhome.Blog.Entity.Weblog_Log WL = new Ncuhome.Blog.Entity.Weblog_Log();
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
WL.Log_CateId = 1;
WL.Log_CommentCount = 0;
WL.Log_IsTop = false;
WL.Log_IsVisible = true;
WL.Log_ViewCount = 0;
WL.Log_IP = Globals.IPAddress;
WL.Log_IsDraft = false;
WL.Log_LastModiTime = DateTime.Now;
WL.Log_Title = post.title;
WL.Log_Content = post.description;
WL.Log_UserId = user.User_Id;
WL.Log_CreateTime = DateTime.Now;
WL.Log_IsPasssword = false;
WL.Log_KeyWords = post.title;
int Log_Id = 1;
Ncuhome.Blog.Core.BWeblog_log.Insert(WL,out Log_Id);
return Log_Id.ToString();
}
else
throw new XmlRpcFaultException(0, "用户验证出错!");
}
bool IMetaWeblogApi.UpdatePost(string postid, string username, string password,
Post post, bool publish)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_Log WL = new Ncuhome.Blog.Entity.Weblog_Log();
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
WL.Log_Id = Convert.ToInt32(post.postid);
WL.Log_CateId = 1;
WL.Log_CommentCount = 0;
WL.Log_IsTop = false;
WL.Log_IsVisible = true;
WL.Log_ViewCount = 0;
WL.Log_IP = Globals.IPAddress;
WL.Log_IsDraft = false;
WL.Log_LastModiTime = DateTime.Now;
WL.Log_Title = post.title;
WL.Log_Content = post.description;
WL.Log_UserId = user.User_Id;
WL.Log_CreateTime = DateTime.Now;
WL.Log_IsPasssword = false;
WL.Log_KeyWords = post.title;
Ncuhome.Blog.Core.BWeblog_log.Update(WL);
return true;
}
else
return false;
}
Post IMetaWeblogApi.GetPost(string postid, string username, string password)
{
if (ValidateUser(username, password))
{
Post post = new Post();
return post;
}
throw new XmlRpcFaultException(0, "出错了!");
}
CategoryInfo[] IMetaWeblogApi.GetCategories(string blogid, string username, string password)
{
if (ValidateUser(username, password))
{
List<CategoryInfo> categoryInfos = new List<CategoryInfo>();
int TxzId = GetTxzIdByTxzUID(username);
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
IEnumerable<Ncuhome.Blog.Entity.Weblog_Category> ca = Ncuhome.Blog.Core.BWeblog_Category.SelectByUID(user.User_Id);
CategoryInfo haha = new CategoryInfo();
haha.categoryid = "1";
haha.description = "默认分类";
haha.htmlUrl = "";
haha.rssUrl = "";
haha.title = "默认分类";
categoryInfos.Add(haha);
foreach (var c in ca)
{
CategoryInfo cate = new CategoryInfo();
cate.categoryid = c.Cate_Id.ToString();
cate.description = c.Cate_Name;
cate.htmlUrl = "";
cate.rssUrl = "";
cate.title = c.Cate_Name;
categoryInfos.Add(cate);
}
return categoryInfos.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
Post[] IMetaWeblogApi.GetRecentPosts(string blogid, string username, string password,
int numberOfPosts)
{
if (ValidateUser(username, password))
{
List<Post> posts = new List<Post>();
int TxzId = GetTxzIdByTxzUID(username);
IEnumerable<Ncuhome.Blog.Entity.Weblog_Log> WL = BWeblog_log.GetByBlogUID(TxzId);
foreach (var w in WL)
{
Post p = new Post();
p.categories = new string[] { w.Log_CateId.ToString()};
p.dateCreated = w.Log_CreateTime;
p.description = w.Log_KeyWords;
p.postid = w.Log_Id;
p.title = w.Log_Title;
p.userid = w.Log_UserId.ToString();
posts.Add(p);
}
return posts.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
MediaObjectInfo IMetaWeblogApi.NewMediaObject(string blogid, string username, string password,
MediaObject mediaObject)
{
if (ValidateUser(username, password))
{
MediaObjectInfo objectInfo = new MediaObjectInfo();
return objectInfo;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 删除日志
/// </summary>
/// <param name="key"></param>
/// <param name="postid"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="publish"></param>
/// <returns></returns>
bool IMetaWeblogApi.DeletePost(string key, string postid, string username, string password, bool publish)
{
if (ValidateUser(username, password))
{
bool result = true;
return result;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 博客信息
/// </summary>
/// <param name="key"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
BlogInfo[] IMetaWeblogApi.GetUsersBlogs(string key, string username, string password)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
List<BlogInfo> infoList = new List<BlogInfo>();
Ncuhome.Blog.Entity.Weblog_User user = BWeblog_User.getByTxzID(TxzId);
BlogInfo bi = new BlogInfo();
bi.blogid = user.User_Id.ToString();
bi.blogName = user.User_NickName;
bi.url = "http://blog.ncuhome.cn/"+BlogContext.Current.Owner.User_DomainName+"/log";
infoList.Add(bi);
return infoList.ToArray();
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
/// <summary>
/// 用户信息
/// </summary>
/// <param name="key"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
UserInfo IMetaWeblogApi.GetUserInfo(string key, string username, string password)
{
if (ValidateUser(username, password))
{
int TxzId = GetTxzIdByTxzUID(username);
UserInfo info = new UserInfo();
Ncuhome.Blog.Entity.Weblog_User user = new Ncuhome.Blog.Entity.Weblog_User();
user = BWeblog_User.getByTxzID(Convert.ToInt32( TxzId));
info.email = user.User_MSN;
info.firstname = "";
info.lastname = user.User_Name;
info.nickname = user.User_NickName;
info.userid = user.User_Id.ToString();
return info;
}
throw new XmlRpcFaultException(0, "用户验证出错!");
}
//========================================================================
//==================上面都是实现接口的方法,下面是我们的方法
/// <summary>
/// 用户登陆验证
/// </summary>
/// <param name="TxzId">通行证的ID</param>
/// <param name="TxzPsw">通行证的密码</param>
/// <returns>是否通过验证</returns>
private bool ValidateUser(string TxzId, string password)
{
return Ncuhome.Login.Login.CheckLogin(TxzId, password);
}
private int GetTxzIdByTxzUID(string TxzUID)
{
return BGDXSJBXX.GetTxzIdByTxzUID(TxzUID);
}
}
}
3. MetaWeblogApi.ashx处理程序
只需一行:
<%@ WebHandler Language="C#" Class="Ncuhome.Blog.MetaWeblogApi.MetaWeblogApi" %>
测试发布日志成功,接下来再研究怎么支持上传图片。