DotText实现支持自动发现服务接口

目标:使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值