天天看点

校内网API的.net版本XiaoNei.Net 1.0(非官方)

校内api开放也有一段时间了,也没有太关注,正好新版本的sns开发到了api这一块,正好借鉴一下xiaonei,fb,myspace的api。

 且闻校内api比较有前途,最近的api编程大赛也比较火,而且人气也较旺

于是顺手用了一下校内的api,发现校内只提供了php与jsp的api却并未提供.net。

当然也有不少朋友封装了.net 的api,但我感觉使用上略见复杂,于是我自己封装了一下。

dll文件、源码、演示下载:http://www.codeplex.com/xn/release/projectreleases.aspx?releaseid=16307

codeplex开源项目 :http://www.codeplex.com/xn/

一、最简单的,页面继承xpage类或xmasterpage类

public partial class friends_get : xpage {

    protected void page_load(object sender, eventargs e) {

    }

}

二、如果不符合你的条件的话,可以使当前页面实现ixhandler接口并在调用 xiaoneiapi.init(this)方法

如我要在asp.net mvc中使用xiaonei.net则我用以下代码让controller支持校内网的api:

public class homecontroller:controller,ixhandler{

    public xiaoneiapi api { get; set; }

        public homecontroller(){

               xiaoneiapi.init(this);

        }

三、如果还是习惯那种老式的方法也可以,当然也可以写在page_load里,个人习习惯,不过我感觉这样的话代码真是不少

public class xpage : system.web.ui.page{

    protected override void oninit(eventargs e) {

        base.oninit(e);

                string secret = request.querystring["xn_sig_session_key"].tostring();

                secret = server.urlencode(secret);

            string apikey = request.querystring["xn_sig_api_key"];

            this.api = new xiaoneiapi(apikey, secret);

       }

 在初始化之后,即默认你继承了xpage

则页面类中会有一个api属性。

如果你想调用users.getinfo方法,则api.users.getinfo()就可以得到

如果你想调用friends.arefriends方法则api.friends.arefriends("","")进行判断

具体的使用方法可以在演示中看到

 数据库访问:

这里我做了一个比较方便的方法

        databaseexecutor dbe = new databaseexecutor(

            new oledbdataopen(connectionstring)

            );

        datalist1.datasource = dbe.gettable("select * from [user] where role=@r and [type]=@t"

                                                     ,"@r",1

                                                     ,"@t","admin");

 databaseexecutor 除了可以用gettable外还有直接执行的execute方法以及返回首行首列的executescalar方法

 databaseexecutor使用什么数据库适配器全在于它的构造函数,在程序中我也提供了一个sqlserver的类sqldataopen。使用时只要将上面的oledbdataopen替换就行了。

本程序为校内网应用程序,请登录后再使用:通常在本地调试时出现,只要在url的querystring加上你的 xn_sig_session_key与xn_sig_api_key就行了获取的方法是在你校内应用上点右键,得到你iframe中的地址,看看它的查 询字符串(就是?后面的内容),复制过来帖在地址后面即可

其它说明会一点一点补充的

另:

多谢 许哲兄 是他的api的封装使我产生了做这个的念头

命名与基本的初始化都始于许哲兄的api

校内群http://group.xiaonei.com/gettribe.do?id=249310165

开发者:http://www.cnblogs.com/chsword/

本程序完全开源可以随意更改,可以不保留作者信息。