給自己的Blog程式添加對Windows Live Writer的支援

來源:互聯網
上載者:User
[from http://sanyuexx.spaces.live.com/Blog/cns!BF32CCAAE...] Windows Live Writer以及Xml-Rpc的介紹

Windows Live Writer是Microsoft推出的瀏覽器外用的Blog編輯和發布的工具, 支援Html和簡單的圖片編輯上傳, 使用它, 可以極大的方便Blog的編輯和發布. 現在Windows Live Writer對國外較大的Blog服務提供了支援, 如Live Spaces, Blogger等, 如果你沒用使用支援列表裡的服務卻想使用Windows Live Writer的話, 只要給你的BLog添加相應的Xml-Rpc支援就可以了.

Xml-Rpc是類似WebService的一種遠程調用服務, 但支援它的平台要比WebService要多, Windows Live Writer以及其他很多離線Blog編輯發布工具都是使用Xml-Rpc來調用Blog程式的相應方法.

本文以ASP.NET為例,給Blog添加對Windows Live Writer的Xml-Rpc的支援,讓我們可以在Live Writer裡面實現編輯, 發布和修改Blog條目, 如果你的Blog是使用其他平台的話, 請參看

http://www.xmlrpc.com/directory/1568/implementatio... 來尋找如何在你的平台實現Xml-Rpc. 雖然在各平台實現Xml-Rpc的方法不同, 但是添加對Windows Live Writer支援的介面的步驟還是差不多的.

準備工作

對於ASP.NET平台, 首先在http://www.xml-rpc.net/ 下載對Xml-Rpc支援的組件, 解壓縮後把bin檔案夾中的 CookComputing.XmlRpcV2.dll 複製到ASP.NET程式的bin檔案夾, 並且把interface檔案夾中的 MetaWeblogAPI.cs 複製到App_Code檔案夾(當然ASP.NET程式的其他檔案夾也可以)..

開始

首先修改MetaWeblogAPI.cs檔案, 這是Windows Live Writer支援的介面之一, 但是必須對它加一個方法才能正常的支援Windows Live Writer

添加一個struct:

public struct UserBlog { public string url; public string blogid; public string blogName; }

這是用來儲存Blog資訊的結構

然後再IMetaWeblog介面中添加一個方法:

[XmlRpcMethod("blogger.getUsersBlogs")] UserBlog[] getUsersBlogs(string appKey, string username, string password);

XmlRpcMethod指定的是在Xml-Rpc中的方法名, 這個不能改的..Client無法識別和調用

介面檔案準備好了, 現在要實現介面的方法了

在添加在ASP.NET程式根目錄添加一個ashx檔案, 假設是Rpc.ashx, 下面提供一個Rpc.ashx的例子:

<%@ WebHandler Language="C#" Class="RPCService" %> <!--註冊類--><%@ Assembly Name="CookComputing.XmlRpcV2" %> <!--註冊實現XmlRpc用的CookComputing.XmlRpcV2.dll-->using System;using System.Collections;using System.Web;using CookComputing.XmlRpc; //引入XmlRpcusing CookComputing.MetaWeblog; //引入IMetaWeblog介面using DataAccess; //引入你自己的Blog程式的資料訪問namespace[XmlRpcService(Description = "MetaWeblog XML-RPC Service")]public class RPCService : XmlRpcService,IMetaWeblog{ #region IMetaWeblog 成員 //獲得Blog網站資訊 public UserBlog[] getUsersBlogs(string appKey, string username, string password) { if (Login(username, password)) //驗證使用者名稱密碼, 你必須在這個類中自己實現這個方法 { UserBlog blog = new UserBlog(); blog.blogid = ""; //就一個Blog, 這個不用 blog.blogName = "LifeBeta"; //BlogName, 隨便寫吧 blog.url = "http://aspspider.org/lifebeta/"; //Blog的網址 return new UserBlog[] { blog }; } return null; } //添加Blog public string newPost(string blogid, string username, string password, Post post, bool publish) { if (Login(username, password)) { //使用自己的DataAccess實現 return DataAccess.AddBlog(post.title, post.description, 4, "", false, DateTime.Now.AddHours(12)).ToString(); } return null; } //編輯Blog public object editPost(string postid, string username, string password, Post post, bool publish) { if (Login(username, password)) { //使用你自己的DataAccess來實現, 注意post.title是標題, post.description是Blog的內容 DataAccess.EditBlog(Convert.ToInt32(postid), post.title, post.description, 4, "", false); } return null; } //擷取分類 public CategoryInfo[] getCategories(string blogid, string username, string password) { if (Login(username, password)) { //略 } return null; } //擷取文章 public Post getPost(string postid, string username, string password) { if (Login(username, password)) { //略 } return new Post(); }

//擷取最近發表的文章

public Post[] getRecentPosts(string blogid, string username, string password, int numberOfPosts) { if (Login(username, password)) { //略 } return null; } //發布媒體對象 public UrlData newMediaObject(string blogid, string username, string password, FileData file) { if (Login(username, password)) return new UrlData(); } return new UrlData(); } //自訂方法

public bool Login(string userName, string pass) { //略,用來驗證使用者 } #endregion}

OK..差不多就這樣了..在瀏覽器裡輸入這個ashx看看, 有以上實現的方法列表和資料類型, 挺像WebService的本地測試頁面吧. 不過Xml-Rpc不能本地測試. 你現在就可以用Windows Live Writer來試試它了.

使用Windows Live Writer發布Blog

和添加其他Blog網站一樣, 不過在選擇Blog服務的時候要選擇MetaWeblog(Custom), script的地址填ashx檔案的路徑.

大功告成!  因為使用了別人做好的Xml-Rpc解決方案, 所以要實現它還是挺簡單的. 由於Live Writer尚處於beta階段, 還是有些不完善的地方, .. 期待Live Writer的正式版吧..

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.