C#非同步呼叫飛信WebService即時接收簡訊

來源:互聯網
上載者:User

第一步:

      新建立一Win視窗項目,並添加Web引用飛信WebService,引用地址:http://io.feirobot.cn/fWebSer.asmx?WSDL,重新命名引用為:FetionObj

第二步:

      添加以下相關控制項到主視窗

1、加TextBox控制項用於顯示收到的相關資訊 命名:showtxt,設此控制項Multiline=true,使能顯示多行,並拖大些。

2、加TextBox控制項用於輸入登入號碼 命名:loginNo。

3、加TextBox控制項用於輸入登入密碼 命名:loginPass。

4、加一Button控制項命名Login,設定Text=登入,Click事件指向Login_Click函數。

第三步:

      編寫相關代碼

1、在代碼最上面添加引用:

using FetionObj.fWebSer;

2、以下是非同步呼叫的相關源碼:

        private string _myMNo=""; //儲存登入號

        private fWebSer fws=null; //聲名介面類

        private bool reAsync=false; //連續接收資訊開關

        public Form1()
        {
            InitializeComponent();

            fws = new fWebSer(); //建立WebService介面類
            fws.CookieContainer = new System.Net.CookieContainer(); //這句很重要,否則將不能登入
        }

        public bool Login_Click(object sender, EventArgs e)
        {
                _myMNo = loginNo.Text;

              
                fws.Login(_myMNo, loginPass.Text); //要先發送登入命令
                if (!reAsync) //接收已經停止才能重新開始非同步接收
                {
                    reAsync = true;
                    DelegateTreCom dn = new DelegateTreCom(TreCom); //執行個體化委託並初賦值
                    IAsyncResult iar = dn.BeginInvoke(_myMNo, new AsyncCallback(CallBackTreCom), dn); //非同步開始執行
                }
        }

        //多安全執行緒委託輸出
        private delegate void MyControl(Control myc, string txt);
        private void Setout(Control myc, string txt)
        {
                if (myc.InvokeRequired)
                    myc.Invoke(new MyControl(Setout), myc, txt);
                else
                    myc.Text = txt + "\r\n" + myc.Text;
        }

        private delegate void DelegateTreCom(string recom); //定義委託
        private void TreCom(string myMNo)
        {//用非同步處理收到的資訊的函數
            try
            {
                string remsg=fws.ReMsg(myMNo, 40); //長串連方式接收資料,沒資料時最多可以等待40秒再返回
                string[] rearr = remsg.Split('\n');  //拆分事件記錄
                for (int i = 0; i < rearr.Length; i++)  //處理收到的所有事件
                {
                    //分析每一事件
                    string com = "", rfno = "", rmno = "", msg = "", errtxt = "", rgno = "", fname = "";
                    string[] comarr = rearr[i].Split('&');
                    for (int j = 0; j < comarr.Length; j++)
                    {
                        string[] varr = comarr[j].Split('=');
                        if (varr.Length < 2) continue;
                        if (varr[0] == "com") com = varr[1];
                        if (varr[0] == "rfno") rfno = varr[1];
                        if (varr[0] == "rmno") rmno = varr[1];
                        if (varr[0] == "msg") msg = varr[1];
                        if (varr[0] == "errtxt") errtxt = varr[1];
                        if (varr[0] == "rgno") rgno = varr[1];
                        if (varr[0] == "fname") fname = varr[1];
                    }
                    if (com == "rmsg")
                    {//收到新資訊
                        Setout(showtxt,"收到"+ fname+"("+rfno+"|"+rmno+")新資訊:"+msg);
                    }
                    if (com == "sendok")
                    {//發送成功

                        Setout(showtxt,"發送到"+ rfno+"成功,內容:"+msg);
                    }
                    if (com == "senderr")
                    {//發送失敗
                        Setout(showtxt,"發送到"+ rfno+"失敗,錯誤資訊:"+errtxt);
                    }
                    if (com == "loginok")
                    {//登入成功

                        Setout(showtxt,"登入成功,自己的飛訊號:"+ rfno+",手機號:"+rmno+",暱稱:"+fname);
                    }
                    if (com == "loginerr")
                    {//登入失敗
                        reAsync = false;  //停止接收事件

                        Setout(showtxt,"登入失敗");
                    }
                    if (com == "fadd")
                    {//被添加為好友

                        Setout(showtxt,"被添加為好友,對方資訊:"+msg.Trim());
                    }
                    if (com == "list")
                    {//好友名單完成

                        Setout(showtxt,"好友名單完成");
                    }
                    if (com == "logout")
                    {//被登出
                        reAsync = false;  //停止接收事件
                        Setout(showtxt,"已經登出");
                    }
                    if (com == "changed")
                    {//好友狀態改變

                        string[] farr = msg.Trim().Split('|');
                        if (farr.Length >= 7)
                        {
                            if (farr[5] == "聊天")
                            {
                                Setout(showtxt,"好友開啟聊天視窗:"+msg.Trim());
                            }
                            else
                            {
                                Setout(showtxt,"好友狀態改變:"+msg.Trim());
                            }
                        }
                    }
                }
            }
            catch (Exception er) {
                Setout(showtxt,"接收出錯:"+er.Message+er.StackTrace);
            }
            if (reAsync)
            {//重複調用接收資料
                Thread.Sleep(2000); //控制調用速度
                DelegateTreCom dn = new DelegateTreCom(TreCom); //執行個體化委託並初賦值
                IAsyncResult iar = dn.BeginInvoke(myMNo, new AsyncCallback(CallBackTreCom), dn); //非同步開始執行
            }
        }
        //非同步完成時,執行的方法(回調方法)

        private void CallBackTreCom(IAsyncResult ar)
        {
            //從非同步狀態ar.AsyncState中,擷取委派物件
            DelegateTreCom dn = (DelegateTreCom)ar.AsyncState;
            //一定要EndInvoke,否則...
            dn.EndInvoke(ar);
        }

 其它象發送資訊等調用就簡單了,大家自己添加控制項實現吧

原文:http://blog.csdn.net/mywwl/archive/2009/05/11/4169455.aspx

相關文章

聯繫我們

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