天天看點

校内網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/

本程式完全開源可以随意更改,可以不保留作者資訊。