目标:使用Live Writer添加Dottext Blog帐号时支持自动发现webservice接口。实现后的添加只要一个步骤:输入blog地址,例如http://blogs.impx.net/dragonimp和blog帐号密码,点下一步自动配置service的api,完成即可。
实现:根据自动发现接口协议,实现对Dottext blog的service api的自动发现。
1、首先在default.aspx中增加link:
<link id="RSDLink" title="rsd" type="application/rsd+xml" rel="edituri" runat="Server"/>
2、在DottextMasterPage.cs中增加地址设置
RSDLink.Attributes.Add("href", CurrentBlog.FullyQualifiedUrl + "rsd.aspx");
3、在web.config中增加rsd.aspx的处理引擎
<HttpHandler pattern="/rsd/.aspx$" type="Dottext.Common.Syndication.RsdHandler, Dottext.Common" handlerType="Direct" />
4、实现RsdHandler类和RsdWriter(采用Rss的写法并继承原有的BaseSyndicationHandler类)
/*
using something needed...
Description:Rsd handler for DotText metaweblog api
Author:dragonimp 2009.1.11
http://www.impx.net http://blogs.impx.net/dragonimp
*/
namespace Dottext.Common.Syndication
{
class RsdWriter : System.Xml.XmlTextWriter
{
private StringWriter _sw = null;
protected BlogConfig config;
public RsdWriter(): this(new StringWriter())
{
}
public RsdWriter(System.IO.Stream stream, Encoding encoding) : base(stream, encoding)
{
}
public RsdWriter(StringWriter sw): base(sw)
{
_sw = sw;
config = Dottext.Framework.Configuration.Config.CurrentBlog();
}
public StringWriter StringWriter
{
get
{
Build();
return _sw;
}
}
public string GetXml
{
get { return this.StringWriter.ToString(); }
}
private void Build()
{
//this.Formatting = System.Xml.Formatting.Indented;
// this.WriteStartDocument();
this.WriteStartElement("rsd");
this.WriteAttributeString("version", "1.0");
this.WriteStartElement("servie");
this.WriteElementString("engineName","DotText Rsd service 1.0 by dragonimp");
//this.WriteEndElement();//enginename
this.WriteElementString("engineLink", config.FullyQualifiedUrl + "rsd.aspx");
//this.WriteEndElement();//enginelink
this.WriteElementString("homePageLink", config.FullyQualifiedUrl);
//this.WriteEndElement();//homepagelinke
this.WriteStartElement("apis");
this.WriteStartElement("api");
this.WriteAttributeString("name", "MetaWeblog");
this.WriteAttributeString("preferred", "true");
this.WriteAttributeString("apiLink", config.FullyQualifiedUrl + @"services/metablogapi.aspx");
this.WriteAttributeString("blogID", config.UserName);
this.WriteEndElement();//api
this.WriteEndElement();//apis
this.WriteEndElement();//service
this.WriteEndElement();//rsd
// this.WriteEndDocument();//
}
}
class RsdHandler : Dottext.Framework.Syndication.BaseSyndicationHandler
{
public RsdHandler() { }
private string key = "IndividualMainFeed_Rsd:FQU{0}";
protected override string CacheKey()
{
return string.Format(key,CurrentBlog.FullyQualifiedUrl);
}
protected override void Cache(CachedFeed feed)
{
Context.Cache.Insert(CacheKey(), feed, null, DateTime.Now.AddSeconds((double)Dottext.Common.Data.CacheTime.Medium), TimeSpan.Zero);
}
protected override CachedFeed BuildFeed()
{
this.key += "BlogID_Rsd" + CurrentBlog.BlogID.ToString();
CachedFeed feed = new CachedFeed();
feed.LastModified = this.ConvertLastUpdatedDate(CurrentBlog.LastUpdated);
RsdWriter rsdwriter = new RsdWriter();
feed.Xml=rsdwriter.GetXml;
return feed;
}
}
}
参考文章:
http://www.tiray.net/post/Really-Simple-Discovery.aspx
http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html