Asp.Net 建立MetaWeblog API

來源:互聯網
上載者:User

       毫無疑問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,  然後添加引用到項目中。。

 

 

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 Structspublic 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 Constructorspublic MetaWeblog(){}#endregion#region IMetaWeblog Membersstring 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 Methodsprivate bool ValidateUser(string username, string password){bool result = false;// TODO: Implement the logic to validate the userreturn result;}#endregion}}
 

6.編譯通過了,測試下: http://localhost:1269/MetaWeblogAPI.ashx

 

 

測試是通過了,具體如何怎麼用在部落格上我也沒做出來,還得研究。值得提醒的是 通過 http://www.xmlrpc.com 下載 的 xml-rpc.net 包包已經包含各個結構,介面和方法的代碼,請自行研究。

下載[ 62KB ]

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.